[英]Disable only specific flat rate shipping method when free shipping is available in WooCommerce
I have two flat rates on my WooCommerce site and I want to disable one of them, when free shipping is enabled.我的 WooCommerce 网站上有两个统一费率,当启用免费送货时,我想禁用其中一个。 I have a function which is working for all flat rates only.我有一个 function 仅适用于所有统一费率。
How do I check for an instance id on a shipping rate?如何检查运费中的实例 ID? Can anyone help me understand how to check for the instance id?谁能帮我理解如何检查实例ID?
I Was trying to echo shipping data out to make sense of what values were available but no echo was showing on the front end either so a tip on why would be great.我试图回显运输数据以了解可用的值,但前端也没有显示回显,因此提示为什么会很棒。
Here is my code attempt:这是我的代码尝试:
function hide_shipping_when_free_is_available( $rates, $package ) {
$new_rates = array();
foreach ( $rates as $rate_id => $rate ) {
// Only modify rates if free_shipping is present.
if ( 'free_shipping' === $rate->method_id ) {
$new_rates[ $rate_id ] = $rate;
break;
}
}
if ( ! empty( $new_rates ) ) {
//Save local pickup if it's present.
foreach ( $rates as $rate_id => $rate ) {
if ('local_pickup' === $rate->method_id ) {
$new_rates[ $rate_id ] = $rate;
}
if ( 'flat_rate:20' === $rate->instance_id )
$new_rates[ $rate_id ] = $rate;
}
return $new_rates;
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );
If you inspect the shipping methods radio buttons on cart or in checkout you will see something like:如果您检查购物车或结帐时的运输方式单选按钮,您将看到如下内容:
<input type="radio" name="shipping_method[0]" data-index="0" id="shipping_method_0_flat_rate20" value="flat_rate:20" class="shipping_method" checked="checked">
So in value="flat_rate:20"
(which is the rate Id) :所以在value="flat_rate:20"
(这是速率 ID) :
flat_rate
,方法 ID是flat_rate
,20
.实例 ID为20
。Note: The code could be really simplified…注意:代码可以真正简化......
But as you don't provide the free shipping and other flat rate rates IDs (or instances Ids) , I Keep your code making some changes to it, this way:但是由于您不提供免费送货和其他统一费率 ID (或实例 ID) ,我会保留您的代码对其进行一些更改,这样:
function hide_shipping_when_free_is_available( $rates, $package ) {
$new_rates = array();
foreach ( $rates as $rate_id => $rate ) {
// Only modify rates if free_shipping is present.
if ( 'free_shipping' === $rate->method_id ) {
$new_rates[ $rate_id ] = $rate;
break;
}
}
if ( ! empty( $new_rates ) ) {
foreach ( $rates as $rate_id => $rate ) {
//Save local pickup if it's present.
if ('local_pickup' === $rate->method_id ) {
$new_rates[ $rate_id ] = $rate;
}
if ( 20 == $rate->instance_id )
$new_rates[ $rate_id ] = $rate;
}
}
return $new_rates;
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );
It should work.它应该工作。
You can check if free shipping is available based on its id.您可以根据其 ID 检查是否可以免费送货。 If it is available, it removes the shipping rate flat_rate:20
.如果可用,它会删除运费flat_rate:20
。
Replace
free_shipping:2
with your free shipping id.将free_shipping:2
替换为您的免费送货 ID。
// if free shipping is available, disable a specific shipping rate
add_filter( 'woocommerce_package_rates', 'hide_specific_shipping_rate_if_shipping_free_is_available', 10, 2 );
function hide_specific_shipping_rate_if_shipping_free_is_available( $rates, $package ) {
if ( isset( $rates['free_shipping:2'] ) ) {
unset( $rates['flat_rate:20'] );
}
return $rates;
}
The code has been tested and works.该代码已经过测试并且可以工作。 Add it to your active theme's functions.php.将其添加到您的活动主题的功能中。php。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.