Hide specifics Flat Rates when Free Shipping is available in WooCommerce 3(在 WooCommerce 3 中提供免费送货时隐藏特定的统一费率)
问题描述
在 WooCommerce 3 中,我有以下运输选项(设置):
In WooCommerce 3, I have these shipping options (settings):
- 免运费:
free_shipping:1
- 最低订单金额设为$50
. - 正常运输
flat_rate:3
- 金额$5
. - 快递
flat_rate:5
- 金额$10
.
- Free Shipping:
free_shipping:1
- Minimum order amount is set at$50
. - Normal Shipping
flat_rate:3
- Amount$5
. - 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 中提供免费送货时隐藏特定的统一
基础教程推荐
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01