-
-
Save rabinkumarpal/93af8375c94d71d5865a1dbcaf8034c2 to your computer and use it in GitHub Desktop.
WooCommerce: Prevent multiple coupons of the same "type" on a single order. This will, for instance, prevent coupon stacking if a customer or user has access to multiple Product % Discount or Cart % Discount coupons. Tested working with WooCommerce, WooCommerce Smart Coupons, and WooCommerce Points & Rewards
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Hook when a coupon is applied to a cart | |
add_action( 'woocommerce_applied_coupon', 'mwd_get_applied_coupons' ); | |
// Get the current applied coupon and compare it with other applied coupons | |
function mwd_get_applied_coupons() { | |
// Get the currently applied coupon's type using the $_POST global to retrieve the current coupon's code | |
foreach ( WC()->cart->get_coupons() as $code => $coupon ) { | |
if($coupon->code == $_POST['coupon_code']) { | |
// Get this coupon's type so we can remove all other coupons of this type from the cart | |
$type_to_remove = $coupon->discount_type; | |
} | |
} | |
// Remove any other coupons of this type | |
// Loop through all other applied coupons | |
foreach ( WC()->cart->get_coupons() as $code => $coupon ) { | |
// Check whether each coupon matches the type of the coupon we just applied | |
if($coupon->code != $_POST['coupon_code'] && $coupon->discount_type == $type_to_remove) { | |
// Exclude "smart_coupon" type, since users should be able to use as many gift cards as they like | |
if($coupon->discount_type != 'smart_coupon') { | |
// Remove all coupons that match the type of the coupon we just applied | |
WC()->cart->remove_coupon( $coupon->code ); | |
} | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment