根据运送类别有条件隐藏WooCommerce运送方式 [英] Conditionally Hide WooCommerce Shipping methods based on shipping class

查看:59
本文介绍了根据运送类别有条件隐藏WooCommerce运送方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一些代码来完成

  • 当购物车中存在超重"运输类别时,隐藏免费运输和固定费用
  • 如果该类不存在,请隐藏超重"运输方法.(163是运输类的ID)

这是代码:

  add_filter('woocommerce_package_rates','wf_hide_shipping_method_based_on_shipping_class',100,2);函数wf_hide_shipping_method_based_on_shipping_class($ available_shipping_methods,$ package){$ hide_when_shipping_class_exist =数组(163 =>大批('flat_rate:1','flat_rate:2','free_shipping:3','free_shipping:5'));$ hide_when_shipping_class_not_exist =数组(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){未设置($ 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){未设置($ available_shipping_methods [$ current_method]);}}}返回$ 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: (无需任何插件,只需设置和代码)

下面的功能将始终显示加拿大的本地取件"运输,并且将:

  1. 当超大"运输类别在购物车中时,隐藏免费运输方式.对于统一费率"运输方式,费用将是特大型"运输类别的一套费用.
  2. 如果未在购物车项目中设置超大"运输类别:
    • 如果购物车数量少于目标免费送货量:隐藏免费送货".
    • 如果购物车金额超过目标免费送货金额:隐藏固定费率"送货方式.

这是代码:

  add_filter('woocommerce_package_rates','wf_hide_shipping_method_based_on_shipping_class',100,2);函数wf_hide_shipping_method_based_on_shipping_class($ rates,$ package){//定义&初始化变量$ free_shipping_rates =数组('free_shipping:3','free_shipping:5');//定义&初始化变量$ shipping_class_id = 163;$ free = array();$ over_found = $ has_free = false;//检查购物车中是否有大件"运输类(163)foreach(WC()-> cart-> get_cart()as $ key => $ cart_item){if($ cart_item ['data']-> get_shipping_class_id()== $ shipping_class_id){$ over_found = true;休息;}}//1.隐藏免费送货,但始终显示加拿大本地提货if($ over_found){foreach($ free_shipping_rates as $ rate_id){unset($ rates [$ rate_id]);}}//2.隐藏固定费用或免费送货->取决于购物车数量//(但始终显示加拿大的本地取件)别的 {foreach($ rates为$ rate_id => $ rate){//当免费送货"可用时,隐藏所有固定费率"if('free_shipping'=== $ rate-> method_id){$ free [$ rate_id] = $ rate;$ has_free = true;} elseif('local_pickup'=== $ rate-> method_id){$ free [$ rate_id] = $ rate;}}返回$ has_free吗?$ free:$ rates;}返回$ rates;} 

代码会出现在您活动的子主题(或主题)的function.php文件或任何插件文件中.

在WooCommerce 3上进行了测试,并且可以正常工作.


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

Using WooCommerce v3.2.4 on This site (here) (WP v4.9) and 11 products with the shipping class of Overweight/Oversize that have a flat rate applied to them:
$20 to Canada and $25 to the US.

All other products have flat rate shipping of $10 (Canada) and $15 (US), unless the order is over $100, then free shipping is applied automatically.

My client wants free shipping to be disabled if there are any overweight/oversize items in the cart. The problem is that the cart says there are no shipping methods available when there are a mix of regular and oversize items in the cart, and no shipping methods are applied.

I'm using XAdapter Woocommerce Shipping Table Rate plugin to apply the higher cost to the "Overweight" Shipping Classes.

UPDATE

I deactivated this plugin, as I realized I could just use the WooCommerce Shipping Zone settings to set a flat rate for specific shipping classes. See screenshot below:

I am using some code to:

  • hide the free shipping and flat rates when the "Overweight" Shipping Class exists in the cart
  • hide the "Overweight" Shipping Method if that class does not exist (163 being the id of the Shipping Class)

Here is that code:

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

Here is a list of IDs of the rates per shipping zone:

Canada

  • Regular Flat rate | ID: flat_rate:1
  • Free Shipping | ID: free_shipping:3
  • Local Pickup | ID: local_pickup:4

USA

  • Regular Flat rate | ID: flat_rate:2
  • Free Shipping | ID: free_shipping:5

解决方案

UPDATE 2: (Without any plugin need, just settings and code)

The function below will always show "Local pickup" shipping for canada and will:

  1. Hide free shipping methods when "Oversize" shipping class is in cart items. For the "Flat rate" Shipping Methods, the cost will be the one set for "Oversize" shipping class.
  2. if "Oversize" Shipping Class is not set in cart items:
    • If cart amount is less than the target free shipping amount: Hide "Free shipping".
    • If cart amount is over the target free shipping amount: Hide "Flat rate" Shipping Methods.

Here is that code:

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;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested on WooCommerce 3 and works.


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运送方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆