有条件地隐藏基于运输的 WooCommerce 运输方式 class

Conditionally Hide WooCommerce Shipping methods based on shipping class

This site (here) (WP v4.9) 上使用 WooCommerce v3.2.4 和 11 种产品 Overweight/Oversize 的运费 class 应用统一费率:
20 美元 到加拿大,25 美元 到美国。

所有其他产品的统一运费为 10 美元(加拿大)和 15 美元(美国),除非订单已结束 [​​= 45=]100 美元,然后自动应用免费送货。

如果购物车中有 overweight/oversize 件商品,我的客户希望禁用免费送货。问题是,当购物车中混合了普通商品和超大商品时,购物车显示没有可用的送货方式,并且没有应用任何送货方式。

我正在使用 XAdapter Woocommerce Shipping Table Rate 插件将更高的成本应用于 "Overweight" 运费 Classes。

更新

我停用了这个插件,因为我意识到我可以只使用 WooCommerce Shipping Zone 设置来为特定的运输设置固定费率 classes。请参见下面的屏幕截图:

我正在使用一些代码来:

代码如下:

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 列表:

加拿大

美国

更新 2: (无需任何插件,只需设置和代码)

下面的函数将始终显示 "Local pickup" 加拿大的运费,并将:

  1. 当 "Oversize" 送货 class 在购物车中时,隐藏免费送货方式。对于 "Flat rate" 运输方式,费用将是 "Oversize" 运输方式 class 的一套。
  2. if "Oversize" 运费 Class 未在购物车商品中设置:
    • 如果购物车数量少于目标免费送货数量:隐藏"Free shipping"。
    • 如果购物车数量超过目标免费送货数量:隐藏"Flat rate"送货方式。

代码如下:

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.

这可以通过使用 Woocommerce Shipping Table Rate 来实现,您只需要添加额外的规则。您需要添加 7 条运输规则,您可以在下图中看到运输规则: Image contains the shipping rule you need to add

对于上述规则,您将在购物车页面获得想要的结果。我附上了不同情况下购物车页面的截图,供大家参考: 注意:运输 class 帽子:常规和连帽衫:超重。

Regular and overweight product ordered together

Over wight product with orders over 0

Free shipping for orders amount over 0 with regular items