在WooCommerce 3提供免费送货时隐藏具体的统一费率 [英] Hide specifics Flat Rates when Free Shipping is available in WooCommerce 3

查看:78
本文介绍了在WooCommerce 3提供免费送货时隐藏具体的统一费率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WooCommerce 3中,我有以下运输选项(设置):

In WooCommerce 3, I have these shipping options (settings):


  1. 免费送货:免费送货:1 -最小订单金额设置为 $ 50

  2. 常规运费 flat_rate: 3 -金额 $ 5

  3. 特快专递 flat_rate:5 -金额 $ 10

  1. Free Shipping: free_shipping:1 - Minimum order amount is set at $50.
  2. Normal Shipping flat_rate:3 - Amount $5.
  3. Express Shipping flat_rate:5 - Amount $10.

我要快捷送货选项将始终可用(显示)。

I would like Express Shipping option to be always available (shown).

但是,当免费送货可用时(意味着客户购物车中的物品超过$ 50),我想隐藏正常仅运送

But when Free shipping is available (meaning that the customer has more than $50 in the cart) I would like to hide Normal Shipping only.

因此,当 免运费 可用时(并隐藏),可用的运费将是正常运送和特快运送。

So when Free shipping is NOT available (and hidden), the available shipping rates will be Normal Shipping and Express Shipping.

有可能吗?如何在WooCommerce 3上获取此信息?

Is that possible? How can I get this on WooCommerce 3?

推荐答案

基于 WooCommerce官方代码段,只需进行一些更改,您将只能隐藏免费送货时您的第一个固定费率:

Based on the official WooCommerce snippet code, making some light changes, you will be able to hide only your first flat rate when free shippings is available:

add_filter( 'woocommerce_package_rates', 'conditionally_hide_shipping_methods', 100, 2 );
function conditionally_hide_shipping_methods( $rates, $package ) {
    // HERE yours 2nd flat rate "Express Shipping" (that you never hide) in the array:
    $flat_rates_express = array( 'flat_rate:5', 'flat_rate:12', 'flat_rate:14' );

    $free = $flat2 = array();
    foreach ( $rates as $rate_key => $rate ) {
        // Updated Here To 
        if ( in_array( $rate->id, $flat_rates_express ) )
            $flat2[ $rate_key ] = $rate;
        if ( 'free_shipping' === $rate->method_id )
            $free[ $rate_key ] = $rate;
    }
    return ! empty( $free ) ? array_merge( $free, $flat2 ) : $rates;
}

代码包含在您活动的子主题的function.php文件中(

在WooCommerce 3上测试并可以正常工作。

Tested on WooCommerce 3 and works.


刷新运输缓存:

1)首先清空购物车。

2)此代码已保存在function.php文件中。

3)进入运输区域设置,并禁用一个统一费率 (例如)和保存。然后重新启用该统一费率和保存。 您已经完成,可以对其进行测试。

Refresh the shipping caches:
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 3提供免费送货时隐藏具体的统一费率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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