在 WooCommerce 中有条件地隐藏运输方式
Conditionally hide shipping methods in WooCommerce
I am trying to hide a shipping method on the checkout page of WooCommerce when shipping country selected is United States - 'US' and cart_total is more than 100. But the script is still hiding both Normal Shipping 和 Express shipping,当它应该只隐藏 normal shipping(flat_rate:4)。任何帮助将不胜感激!
function nd_hide_shipping_when_free_is_available( $rates ) {
$shipping_counrtry = WC()->customer->get_shipping_country();
$total = WC()->cart->get_displayed_subtotal();
if ($shipping_country == "US" && $total >= 100){
unset($rates['flat_rate:4']);
return $rates;
}else{
$free = array();
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->method_id ) {
$free[ $rate_id ] = $rate;
break;
}
}
return ! empty( $free ) ? $free : $rates;
}
}
add_filter( 'woocommerce_package_rates', 'nd_hide_shipping_when_free_is_available', 100 );
您可以尝试以下方法,这将隐藏 'flat_rate:4' 美国的送货方式费率 ID,当购物车小计达到或超过 100 时,否则当免费送货可用时,它将隐藏其他送货方式:
add_filter( 'woocommerce_package_rates', 'shipping_based_on_country_subtotal_and_free_available', 100, 2 );
function shipping_based_on_country_subtotal_and_free_available( $rates, $package ) {
$country = WC()->customer->get_shipping_country();
$subtotal = WC()->cart->subtotal; // subtotal incl taxes
$condition = $country == "US" && $subtotal >= 100; // <== HERE Set your condition (country and minimal subtotal amount)
$free = array(); // Initializing
// Loop through shipping rates for current shipping package
foreach ( $rates as $rate_key => $rate ) {
if ( $condition ){
$targeted_rate_id = 'flat_rate:4';
if( $targeted_rate_id === $rate_key ) {
unset($rates[$targeted_rate_id]);
}
}
elseif ( 'free_shipping' === $rate->method_id ) {
$free[$rate_key] = $rate;
}
}
return ! empty( $free ) && ! $condition ? $free : $rates;
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
Important: Once code is saved, empty the cart to refresh shipping rates...
I am trying to hide a shipping method on the checkout page of WooCommerce when shipping country selected is United States - 'US' and cart_total is more than 100. But the script is still hiding both Normal Shipping 和 Express shipping,当它应该只隐藏 normal shipping(flat_rate:4)。任何帮助将不胜感激!
function nd_hide_shipping_when_free_is_available( $rates ) {
$shipping_counrtry = WC()->customer->get_shipping_country();
$total = WC()->cart->get_displayed_subtotal();
if ($shipping_country == "US" && $total >= 100){
unset($rates['flat_rate:4']);
return $rates;
}else{
$free = array();
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->method_id ) {
$free[ $rate_id ] = $rate;
break;
}
}
return ! empty( $free ) ? $free : $rates;
}
}
add_filter( 'woocommerce_package_rates', 'nd_hide_shipping_when_free_is_available', 100 );
您可以尝试以下方法,这将隐藏 'flat_rate:4' 美国的送货方式费率 ID,当购物车小计达到或超过 100 时,否则当免费送货可用时,它将隐藏其他送货方式:
add_filter( 'woocommerce_package_rates', 'shipping_based_on_country_subtotal_and_free_available', 100, 2 );
function shipping_based_on_country_subtotal_and_free_available( $rates, $package ) {
$country = WC()->customer->get_shipping_country();
$subtotal = WC()->cart->subtotal; // subtotal incl taxes
$condition = $country == "US" && $subtotal >= 100; // <== HERE Set your condition (country and minimal subtotal amount)
$free = array(); // Initializing
// Loop through shipping rates for current shipping package
foreach ( $rates as $rate_key => $rate ) {
if ( $condition ){
$targeted_rate_id = 'flat_rate:4';
if( $targeted_rate_id === $rate_key ) {
unset($rates[$targeted_rate_id]);
}
}
elseif ( 'free_shipping' === $rate->method_id ) {
$free[$rate_key] = $rate;
}
}
return ! empty( $free ) && ! $condition ? $free : $rates;
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
Important: Once code is saved, empty the cart to refresh shipping rates...