首页 文章

有条件隐藏WooCommerce运输方法基于运输类

提问于
浏览
4

This site (here)(WP v4.9)上使用WooCommerce v3.2.4和11种产品,其运输类别为Overweight / Oversize,适用于固定费率:
$20 到加拿大和 $25 到美国 .

所有其他产品的统一运费 $10 (加拿大)和 $15 (美国),除非订单超过 $100 ,然后免费送货自动运送 .

如果购物车中有超重/超大商品,我的客户希望免费送货 . 问题是购物车说当购物车中有常规和超大尺寸物品混合时没有可用的运输方法,并且没有运输方法 .

我正在使用XAdapter Woocommerce Shipping Table Rate插件将更高的成本应用于"Overweight"运输类 .

UPDATE

我停用了此插件,因为我意识到我可以使用WooCommerce Shipping Zone设置为特定的运输类别设置固定费率 . 见下面的截图:
Flat Rate Settings for one of my shipping zones

我正在使用一些代码:

  • 当购物车中存在"Overweight"货运等级时,隐藏免运费和统一费率

  • 如果该类不存在,则隐藏"Overweight"装运方法(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;
}

EDIT

以下是每个送货区的费率ID列表:

Canada

  • 普通统一费率| ID: flat_rate:1

  • 免费送货| ID: free_shipping:3

  • 本地取件| ID: local_pickup:4

USA

  • 普通统一费率| ID: flat_rate:2

  • 免费送货| ID: free_shipping:5

1 回答

  • 2

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

    以下功能将始终显示加拿大的“本地取件”运费,并将:

    • 当"Oversize"货运类在购物车商品中时隐藏免费送货方式 . 对于"Flat rate"装运方法,成本将是"Oversize"装运等级的成本 .

    • 如果"Oversize"未在购物车项目中设置装运等级:

    • 如果购物车金额小于目标免费送货金额:隐藏"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上测试并且有效 .


    刷新运输缓存(有时需要):1)首先清空您的购物车 . 2)此代码已保存在function.php文件中 . 3)进入送货区设置并禁用一个“统一费率”(例如)和“保存” . 然后重新启用“统一费率”和“保存” . 你完成了,你可以测试它 .

相关问题