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 中提供免费送货时隐藏特定的统一
基础教程推荐
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
