本文介绍了WooCommerce-免费送货时隐藏其他送货方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当Woocommerce提供免费送货服务时,我想隐藏其他送货方式.
I would like to hide other shipping options when free shipping is available on Woocommerce.
因为最新版本的woocommerce现在仍显示其他送货选项,即使有免费送货选项也是如此.
Because latest version of woocommerce now is still showing other shipping options even if there's FREE shipping option.
请帮助
推荐答案
WooCommerce 2.6+有此最新代码段.您可以使用的
There is this recent code snippet for WooCommerce 2.6+. that you can Use:
add_filter( 'woocommerce_package_rates', 'hide_other_shipping_when_free_is_available', 100, 2 );
function hide_other_shipping_when_free_is_available( $rates, $package ) {
$free = array();
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->method_id ) {
$free[ $rate_id ] = $rate;
break;
}
}
return ! empty( $free ) ? $free : $rates;
}
对于WooCommerce 2.5,您应该尝试以下操作:
For WooCommerce 2.5, You should try this:
add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );
function hide_shipping_when_free_is_available( $rates, $package ) {
// Only modify rates if free_shipping is present
if ( isset( $rates['free_shipping'] ) ) {
// To unset a single rate/method, do the following. This example unsets flat_rate shipping
unset( $rates['flat_rate'] );
// To unset all methods except for free_shipping, do the following
$free_shipping = $rates['free_shipping'];
$rates = array();
$rates['free_shipping'] = $free_shipping;
}
return $rates;
}
将此代码粘贴到活动的子主题或主题中的function.php文件中.
参考:
这篇关于WooCommerce-免费送货时隐藏其他送货方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!