This site (here)(wp v4.9)上使用woocommerce v3.2.4和11种装运等级为超重/超大的产品,适用统一费率:加拿大20美元,美国25美元。
所有其他产品都有10加元(加拿大)和15加元(美国)的统一运费,除非订单超过100加元,否则将自动应用免费运费。
我的客户希望如果购物车中有任何超重/超大物品,可以禁用免费送货。问题是,购物车说,当购物车中有常规和超大物品的混合时,没有可用的运输方法,也没有应用任何运输方法。
我正在使用XAdapter Woocommerce Shipping Table Rate插件将更高的成本应用到“超重”运输类。
更新
我停用了这个插件,因为我意识到我可以使用Woomerce的发货区域设置来为特定的发货类设置一个统一的费率。见以下截图:
php - 根据运送类别有条件隐藏WooCommerce运送方式-LMLPHP
我正在使用一些代码:
当购物车中存在“超重”运费等级时,隐藏免费运费和统一费率
如果该类不存在,则隐藏“超重”装运方法(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文件或任何插件文件中。
在Woocomerce 3和Works上测试。
刷新传送缓存(有时需要):
1)首先清空购物车。
2)此代码已保存在function.php文件中。
3)进入装运区域设置,禁用一个“统一费率”(例如)和“保存”。然后重新启用“固定费率”和“保存”。你完成了,你可以测试它。

09-27 07:54
查看更多