防止WooCommerce优惠券堆积在购物车和结账页面上



我需要防止两个特定的优惠券一起使用。我成功地实现了这个代码,它防止了在购物车页面上堆叠这些优惠券:

add_action( 'woocommerce_before_cart', 'check_coupon_stack' );
function check_coupon_stack() {
$coupon_code_1 = 'mycode1';
$coupon_code_2 = 'mycode2';
if ( WC()->cart->has_discount( $coupon_code1 ) && WC()->cart->has_discount( $coupon_code2) ) {
WC()->cart->remove_coupon( $coupon_code2 );
$notice_text = 'Discount code '.$coupon_code1.' cannot be combined with code '.$coupon_code2.'. Code '.$coupon_code2.' removed.';
wc_print_notice( $notice_text, 'error' );
wc_clear_notices();
}
}

但是,这并不能阻止在购物车页面后面的结账页上堆叠。

我试着简单地添加:

add_action( 'woocommerce_before_checkout_form', 'check_coupon_stack' );

但这并不能在结账页面上起作用。还需要什么?

WooCommerce包含多个应用于优惠券的挂钩,woocommerce_applied_coupon就是其中之一,非常适合您的问题。

此外,您当前的代码只能在一个方向上工作,即当使用$coupon_code_1时,$coupon_code_2将被删除。然而,当你在问题中指出你想阻止两个特定的优惠券一起使用时,这并不是反方向的。

这在我的回答中被考虑在内,所以你得到:

function action_woocommerce_applied_coupon( $coupon_code ) {
// Settings
$coupon_code_1 = 'coupon1';
$coupon_code_2 = 'coupon2';
// Initialize
$combined = array( $coupon_code_1, $coupon_code_2 );
// Checks if coupon code exists in an array
if ( in_array( $coupon_code, $combined ) ) {
// Get applied coupons
$applied_coupons = WC()->cart->get_applied_coupons();
// Computes the difference of arrays
$difference = array_diff( $combined, $applied_coupons );
// When empty
if ( empty( $difference ) ) {
// Shorthand if/else - Get correct coupon to remove
$remove_coupon = $coupon_code == $coupon_code_1 ? $remove_coupon = $coupon_code_2 : $remove_coupon = $coupon_code_1;
// Remove coupon
WC()->cart->remove_coupon( $remove_coupon );
// Clear Notices
wc_clear_notices();
// Error message
$error = sprintf( __( 'Discount code "%1$s" cannot be combined with code "%2$s". Code "%2$s" removed.', 'woocommerce' ), $coupon_code, $remove_coupon );
// Show error
wc_print_notice( $error, 'error' );
}
}
}
add_action( 'woocommerce_applied_coupon', 'action_woocommerce_applied_coupon', 10, 1 );

最新更新