问题描述
在WooCommerce 3中,我有以下运输选项(设置):
In WooCommerce 3, I have these shipping options (settings):
- 免费送货:
免费送货: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?
推荐答案
基于,只需进行一些更改,您将只能隐藏免费送货时您的第一个固定费率:
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.
这篇关于在WooCommerce 3提供免费送货时隐藏具体的统一费率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!