使用This site (here)上的WooCommerce v3.2.4(WP v4.9)和11种产品,其中包含适用于他们的固定费率的超重/超大的运费等级:加拿大20美元,美国25美元.
所有其他产品的统一运费为10美元(加拿大)和15美元(美国),除非订单超过100美元,然后自动运送免费送货.
如果购物车中有超重/超大商品,我的客户希望免费送货.问题是购物车说当购物车中有常规和超大尺寸物品混合时没有可用的运输方法,并且没有运输方法.
我正在使用XAdapter Woocommerce Shipping Table Rate插件将更高的成本应用于“超重”运输类.
UPDATE
我停用了此插件,因为我意识到我可以使用WooCommerce Shipping Zone设置为特定的运输类别设置固定费率.见下面的截图:
我正在使用一些代码:
>购物车中存在“超重”运输类别时,隐藏免运费和统一费率
>如果该类不存在,则隐藏“超重”运输方法(163为运输类的ID)…
这是代码:
add_filter('woocommerce_package_rates', 'wf_hide_shipping_method_based_on_shipping_class', 100, 2);
function wf_hide_shipping_method_based_on_shipping_class($available_shipping_methods, $package){
$hide_when_shipping_class_exist = array(
163 => array(
'flat_rate:1',
'flat_rate:2',
'free_shipping:3',
'free_shipping:5'
)
);
$hide_when_shipping_class_not_exist = array(
163 => array( 'wf_woocommerce_shipping_pro:overweightoversizeoverweight')
);
$shipping_class_in_cart = array();
foreach(WC()->cart->cart_contents as $key => $values) {
$shipping_class_in_cart[] = $values['data']->get_shipping_class_id();
}
foreach($hide_when_shipping_class_exist as $class_id => $methods) {
if(in_array($class_id, $shipping_class_in_cart)){
foreach($methods as & $current_method) {
unset($available_shipping_methods[$current_method]);
}
}
}
foreach($hide_when_shipping_class_not_exist as $class_id => $methods) {
if(!in_array($class_id, $shipping_class_in_cart)){
foreach($methods as & $current_method) {
unset($available_shipping_methods[$current_method]);
}
}
}
return $available_shipping_methods;
}
编辑
以下是每个送货区的费率ID列表:
加拿大
>常规统一费率| ID:flat_rate:1
>免费送货| ID:free_shipping:3
>本地提货| ID:local_pickup:4
美国
>常规统一费率| ID:flat_rate:2
>免费送货| ID:free_shipping:5
解决方法:
更新2 :(没有任何插件需要,只需设置和代码)
以下功能将始终显示加拿大的“本地取件”运费,并将:
>当“超大”装运类在购物车商品中时,隐藏免费送货方式.对于“统一费率”运输方式,成本将是“超大”运输类的成本.
>如果购物车项目中未设置“超大”装运等级:
>如果购物车金额低于目标免费送货金额:隐藏“免运费”.
>如果购物车金额超过目标免费送货金额:隐藏“统一费率”送货方式.
这是代码:
add_filter('woocommerce_package_rates', 'wf_hide_shipping_method_based_on_shipping_class', 100, 2);
function wf_hide_shipping_method_based_on_shipping_class($rates, $package){
// Defining & Initializing variables
$free_shipping_rates = array(
'free_shipping:3',
'free_shipping:5'
);
// Defining & Initializing variables
$shipping_class_id = 163;
$free = array();
$over_found = $has_free = false;
// Check if "Oversize" shipping class (163) is in cart items
foreach(WC()->cart->get_cart() as $key => $cart_item){
if($cart_item['data']->get_shipping_class_id() == $shipping_class_id ){
$over_found = true;
break;
}
}
// 1. Hiding free shipping but always show Local pickup for Canada
if( $over_found ){
foreach($free_shipping_rates as $rate_id) {
unset( $rates[$rate_id] );
}
}
// 2. Hiding Flat rate OR Free shipping --> depending on cart amount
// (but always show Local pickup for Canada)
else {
foreach ( $rates as $rate_id => $rate ) {
// Hide all "Flat rates" when "Free Shipping" is available
if ( 'free_shipping' === $rate->method_id ) {
$free[ $rate_id ] = $rate;
$has_free = true;
} elseif ( 'local_pickup' === $rate->method_id ) {
$free[ $rate_id ] = $rate;
}
}
return $has_free ? $free : $rates;
}
return $rates;
}
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中.
在WooCommerce 3上测试并且有效.
Refresh the shipping caches (needed sometimes):
1) First empty your cart.
2) This code is already saved on your function.php file.
3) Go in a shipping zone settings and disable one “flat rate” (for example) and “save”. Then re-enable that “flat rate” and “save”. You are done and you can test it.