我有两种运输方式。第一个是免费送货,第二个是快递运费的统一运费,我收取额外费用。默认情况下,在购物车中选择快递运输,这导致一些买家混淆我不提供免费送货服务。
是否可以将默认选择的方法更改为免费送货?
我有两种运输方式。第一个是免费送货,第二个是快递运费的统一运费,我收取额外费用。默认情况下,在购物车中选择快递运输,这导致一些买家混淆我不提供免费送货服务。
是否可以将默认选择的方法更改为免费送货?
我认为您只需要为每个运输区域重新排序运输方式,在第一行移动“免费送货”。
如果不起作用,您可以添加以下代码:
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 文件中。测试和工作。
如果您不使用购物车页面并且有一个到结帐的重定向,您将不得不在代码
woocommerce_before_cart
中用woocommerce_before_checkout_form
钩子替换。
功能 test_default_shipping_method($default,$available){ $default_method = 'wcv_pro_vendor_shipping'; //在这里提供默认选择的服务名称 if(array_key_exists($method, $available_methods)) return $default_method; 否则返回 $default_method; }