我试图得到它,以便如果客户要添加优惠券代码(其中任何一个),免费送货选项将消失,并且将实施统一费率费用。- 你会认为这将是一件容易实现的事情,会有 100 种插件和描述的方法来做到这一点,但我还没有找到。我不想为插件支付 89 美元来做这件事
如果他们使用优惠券但消费超过 249 美元,他们仍然可以获得免费送货的资格。我读了一些如何做到这一点的地方,但它需要我获取 POST ID,而最新的 WooCommerce 无法像以前那样使用它,我不知道运输 ID,所以我很迷茫 这是代码
add_filter( 'woocommerce_shipping_packages', function( $packages ) {
$applied_coupons = WC()->session->get( 'applied_coupons', array() );
if ( ! empty( $applied_coupons ) ) {
$free_shipping_id = 'free_shipping:2';
unset($packages[0]['rates'][ $free_shipping_id ]);
}
return $packages;
} );
谢谢
已编辑
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_total = 250; // Minimal subtotal allowing free shipping
// Get needed cart totals
$total_excl_tax = WC()->cart->get_total();
$discount_excl_tax = WC()->cart->get_discount_total();
// Calculating the discounted subtotal including taxes
$discounted_subtotal_incl_taxes = $total_excl_tax - $discount_excl_tax;
$applied_coupons = WC()->cart->get_applied_coupons();
if( sizeof($applied_coupons) > 0 && $discounted_subtotal_incl_taxes < $min_total ) {
foreach ( $rates as $rate_key => $rate ){
// Targeting "Free shipping"
if( 'free_shipping' === $rate->method_id ){
unset($rates[$rate_key]);
}
}
}
return $rates;
}