我为双倍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
时才有效,但隐藏所有运输方式而不是一种。
基于"https://stackoverflow.com/questions/52912181/applied-coupons-disable-free-shipping-conditionally-in-woocommerce/52913005#52913005">答案线程,这是我的代码尝试:
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
}
}
注1:更正错别字!o至e
//$rate->mothod_id
$rate->method_id
注意2:对于免费方法
if( 'free_shipping' === $rate->method_id ){
unset($rates[$rate_key]);
}
或者:带索引:
if( 'free_shipping:7' === $rate_key ){
unset($rates[$rate_key]);
}