Skip to content

Instantly share code, notes, and snippets.

@jorpdesigns
Last active July 12, 2021 16:53
Show Gist options
  • Save jorpdesigns/7f67708ddfd766e17aa9f959fa39e699 to your computer and use it in GitHub Desktop.
Save jorpdesigns/7f67708ddfd766e17aa9f959fa39e699 to your computer and use it in GitHub Desktop.
Snippet to remove coupon from WooCommerce cart
<?php
add_action( 'woocommerce_before_calculate_totals', 'remove_matched_coupons' );
function remove_matched_coupons() {
$coupon_code = 'renewal20'; // Replace with your own coupon code
if ( ! WC()->cart->has_discount( $coupon_code ) ) return;
// REMOVE COUPON WITHOUT ANY CONDITIONS
WC()->cart->remove_coupon( $coupon_code );
wc_clear_notices();
wc_add_notice( __("Sorry, it seems this coupon is not yours.","woocommerce"), 'notice');
// REMOVE COUPON IF PRODUCT ID IS IN CART
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$excludedProducts = array( 186, 187, 188 ); // Replace with your own product ids
if ( in_array( $cart_item['product_id'], $excludedProducts ) ) {
WC()->cart->remove_coupon( $coupon_code );
wc_clear_notices();
wc_add_notice( __("Sorry, it seems this coupon is not yours.","woocommerce"), 'notice');
}
}
// REMOVE COUPON IF CART TOTAL IS LESS THAN 30
if ( WC()->cart->subtotal < 30 ) ) { // Replace with your own total
WC()->cart->remove_coupon( $coupon_code );
wc_clear_notices();
wc_add_notice( __("Sorry, your minimum spend has to be 30 for you to use this coupon.","woocommerce"), 'notice');
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment