应用优惠券时隐藏特定的运输方式
Hide specific shipping methods when coupon is applied
我已经创建了双倍 x2 运费的优惠券代码 "Tiendas" 并禁用免费默认商店送货(订单 > 50€)
此外,优惠券可免运费,但订单价值会增加到 >250 欧元。
我的商店有 3 种送货方式:
flat_rate:1
flat_rate:7
Free_shipping
启用优惠券后,flat_rate:7
和免费送货必须隐藏。 flat_rate:1
应该是可见的,其运费 x2 (4.80 € x 2= 9.60 €)
仅当我输入 flat_rate
而不是 flat_rate:1
时有效,但隐藏所有送货方式而不是一个。
基于 "" 回答线程,这是我的代码尝试:
add_filter( 'woocommerce_package_rates', 'coupons_removes_free_shipping', 10, 2 );
function coupons_removes_free_shipping( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
$min_subtotal = 250; // Minimal subtotal allowing free shipping
// Get needed cart subtotals
$subtotal_excl_tax = WC()->cart->get_subtotal();
$subtotal_incl_tax = $subtotal_excl_tax + WC()->cart->get_subtotal_tax();
$discount_excl_tax = WC()->cart->get_discount_total();
$discount_incl_tax = $discount_total + WC()->cart->get_discount_tax();
// Calculating the discounted subtotal including taxes
$discounted_subtotal_incl_taxes = $subtotal_incl_tax - $discount_incl_tax;
$applied_coupons = WC()->cart->get_applied_coupons();
if( in_array( 'tiendas',$applied_coupons ) && sizeof($applied_coupons) > 0 && $discounted_subtotal_incl_taxes < $min_subtotal ){
foreach ( $rates as $rate_key => $rate ){
// Set 2x cost"
if( $rate->method_id === 'flat_rate:1' ){
// Set 2x of the cost
$rates[$rate_key]->cost = $rates[$rate_key]->cost * 2;}
// Disable "flat_rate:7"
if( $rate->method_id === 'flat_rate:7' ){
unset($rates[$rate_key]);
// Disable "Free shipping"
if( 'free_shipping' === $rate->method_id ){
unset($rates[$rate_key]);
}
}
}
}
return $rates;
}
其实你在那里,你所要做的就是比较$rate_key
而不是$rate->mothod_id
您的代码应如下所示:
if( in_array( 'tiendas',$applied_coupons ) && sizeof($applied_coupons) > 0 && $discounted_subtotal_incl_taxes < $min_subtotal ){
foreach ( $rates as $rate_key => $rate ){
// Set 2x cost"
if( $rate_key === 'flat_rate:1' ){
// Set 2x of the cost
$rates[$rate_key]->cost = $rates[$rate_key]->cost * 2;
}
// Disable "flat_rate:7"
if( $rate_key === 'flat_rate:7' ){
unset($rates[$rate_key]);
}
// Disable "Free shipping"
if( 'free_shipping' === $rate_key ){
unset($rates[$rate_key]);
}
}
}
或者更简单一点:
if( in_array( 'tiendas',$applied_coupons ) && sizeof($applied_coupons) > 0 && $discounted_subtotal_incl_taxes < $min_subtotal ){
unset($rates['flat_rate:7']);
unset($rates['free_shipping']);
foreach ( $rates as $rate_key => $rate ){
if( $rate_key === 'flat_rate:1' ) $rates[$rate_key]->cost = $rates[$rate_key]->cost * 2; // Set 2x of the cost
}
}
我已经创建了双倍 x2 运费的优惠券代码 "Tiendas" 并禁用免费默认商店送货(订单 > 50€)
此外,优惠券可免运费,但订单价值会增加到 >250 欧元。
我的商店有 3 种送货方式:
flat_rate:1
flat_rate:7
Free_shipping
启用优惠券后,flat_rate:7
和免费送货必须隐藏。 flat_rate:1
应该是可见的,其运费 x2 (4.80 € x 2= 9.60 €)
仅当我输入 flat_rate
而不是 flat_rate:1
时有效,但隐藏所有送货方式而不是一个。
基于 "
add_filter( 'woocommerce_package_rates', 'coupons_removes_free_shipping', 10, 2 );
function coupons_removes_free_shipping( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
$min_subtotal = 250; // Minimal subtotal allowing free shipping
// Get needed cart subtotals
$subtotal_excl_tax = WC()->cart->get_subtotal();
$subtotal_incl_tax = $subtotal_excl_tax + WC()->cart->get_subtotal_tax();
$discount_excl_tax = WC()->cart->get_discount_total();
$discount_incl_tax = $discount_total + WC()->cart->get_discount_tax();
// Calculating the discounted subtotal including taxes
$discounted_subtotal_incl_taxes = $subtotal_incl_tax - $discount_incl_tax;
$applied_coupons = WC()->cart->get_applied_coupons();
if( in_array( 'tiendas',$applied_coupons ) && sizeof($applied_coupons) > 0 && $discounted_subtotal_incl_taxes < $min_subtotal ){
foreach ( $rates as $rate_key => $rate ){
// Set 2x cost"
if( $rate->method_id === 'flat_rate:1' ){
// Set 2x of the cost
$rates[$rate_key]->cost = $rates[$rate_key]->cost * 2;}
// Disable "flat_rate:7"
if( $rate->method_id === 'flat_rate:7' ){
unset($rates[$rate_key]);
// Disable "Free shipping"
if( 'free_shipping' === $rate->method_id ){
unset($rates[$rate_key]);
}
}
}
}
return $rates;
}
其实你在那里,你所要做的就是比较$rate_key
而不是$rate->mothod_id
您的代码应如下所示:
if( in_array( 'tiendas',$applied_coupons ) && sizeof($applied_coupons) > 0 && $discounted_subtotal_incl_taxes < $min_subtotal ){
foreach ( $rates as $rate_key => $rate ){
// Set 2x cost"
if( $rate_key === 'flat_rate:1' ){
// Set 2x of the cost
$rates[$rate_key]->cost = $rates[$rate_key]->cost * 2;
}
// Disable "flat_rate:7"
if( $rate_key === 'flat_rate:7' ){
unset($rates[$rate_key]);
}
// Disable "Free shipping"
if( 'free_shipping' === $rate_key ){
unset($rates[$rate_key]);
}
}
}
或者更简单一点:
if( in_array( 'tiendas',$applied_coupons ) && sizeof($applied_coupons) > 0 && $discounted_subtotal_incl_taxes < $min_subtotal ){
unset($rates['flat_rate:7']);
unset($rates['free_shipping']);
foreach ( $rates as $rate_key => $rate ){
if( $rate_key === 'flat_rate:1' ) $rates[$rate_key]->cost = $rates[$rate_key]->cost * 2; // Set 2x of the cost
}
}