我已经创建了一个优惠券代码“Tiendas”双X2航运价格和禁用免费默认商店航运(订单〉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;
}
2条答案
按热度按时间nqwrtyyt1#
实际上,您已经在那里了,您所要做的就是比较
$rate_key
而不是$rate->mothod_id
您的代码应如下所示:
或者,简单一点:
kulphzqa2#
注1:将排印错误!o更正为e
注2:对于自由方法
或:带有索引: