问题描述
我有两种送货方式.首先是免费送货,其次是特快专递的固定费用送货,为此我要收取额外费用.默认情况下,购物车中选择了快递",这导致一些买家感到困惑,因为我不提供免费送货服务.
I have two shipping methods. First is Free Shipping and second is Flat Rate Shipping for Express shipping for which i charge extra fee. By default Express Shipping is selected in the cart which lead to confusion among some buyers that I do not offer free shipping.
是否可以将默认的选定方法更改为免费送货?
Is it possible to change default selected method to free shipping?
推荐答案
我认为您只需要重新订购每个送货区域的送货方式,就可以免费送货"了.在第一行.
I think that you just need to reorder your shipping methods for each shipping zone, moving "free shipping" on first line.
如果它不起作用,则可以添加以下代码:
If it doesn't work, you can add the following code:
add_action( 'woocommerce_before_cart', 'auto_select_free_shipping_by_default' );
function auto_select_free_shipping_by_default() {
if ( isset(WC()->session) && ! WC()->session->has_session() )
WC()->session->set_customer_session_cookie( true );
// Check if "free shipping" is already set
if ( strpos( WC()->session->get('chosen_shipping_methods')[0], 'free_shipping' ) !== false )
return;
// Loop through shipping methods
foreach( WC()->session->get('shipping_for_package_0')['rates'] as $key => $rate ){
if( $rate->method_id === 'free_shipping' ){
// Set "Free shipping" method
WC()->session->set( 'chosen_shipping_methods', array($rate->id) );
return;
}
}
}
代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试并可以正常工作.
Code goes in function.php file of your active child theme (or active theme). tested and works.
这篇关于更改Woocommerce中的默认送货方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!