【发布时间】:2021-07-05 04:29:55
【问题描述】:
在 WooCommerce 中,我们将 flat_rate 运费设置为 4.95 欧元,free_shipping 显示的最低总金额为 45 欧元。
现在,如果客户有一个购物车 - 比如说 48 欧元 - 他不必支付运费,因为他已达到订单总额以申请免费送货。
如果他现在申请了 10% 的优惠券,他最终将获得 43.20 欧元的订单总额,因此必须再次支付运费。
在该客户应用优惠券并“降落”低于 free_shipping 金额后,我们仍希望为该客户提供免费送货服务。否则使用 10% 的优惠券(在我们的例子中为 4.80 欧元)不是很有吸引力,但必须再次支付 4.95 欧元的运费。
基于Applied coupons disable Free shipping conditionally in Woocommerce 答案代码,这是我的代码尝试:
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;
$shipping_counrtry = WC()->customer->get_shipping_country();
if ($shipping_counrtry == 'DE') : $min_subtotal = 45;
endif;
$shipping_counrtry = WC()->customer->get_shipping_country();
if ($shipping_counrtry == 'AT') : $min_subtotal = 75;
endif;
// 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( sizeof($applied_coupons) > 0 && $discounted_subtotal_incl_taxes > $min_subtotal ){
foreach ( $rates as $rate_key => $rate ){
// Targeting "Free shipping"
if( 'free_shipping' != $rate->method_id ){
// SET THE RATE HERE; but how
}
}
}
return $rates;
}
【问题讨论】:
-
对不起,我正在处理在这个平台上找到的一些代码,但最终无法完成。
标签: php wordpress woocommerce coupon shipping-method