沃梦达 / 编程问答 / php问题 / 正文

在 WooCommerce 3 中提供免费送货时隐藏特定的统一

Hide specifics Flat Rates when Free Shipping is available in WooCommerce 3(在 WooCommerce 3 中提供免费送货时隐藏特定的统一费率)

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

问题描述

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

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

  1. 免运费:free_shipping: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 中提供免费送货时隐藏特定的统一费率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 WooCommerce 3 中提供免费送货时隐藏特定的统一

基础教程推荐