Created
December 10, 2024 04:54
-
-
Save shameemreza/90f10bb6aa3d6b837f934e9b4821cee8 to your computer and use it in GitHub Desktop.
Make mandatory coupons for all products in WooCommerce
This file contains hidden or 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
add_action( 'woocommerce_check_cart_items', 'mandatory_global_coupon_code' ); | |
function mandatory_global_coupon_code() { | |
$coupon_code = 'summer2'; // The required coupon code | |
// Check if the required coupon is applied | |
$coupon_applied = in_array( strtolower( $coupon_code ), WC()->cart->get_applied_coupons() ); | |
// If the coupon is not applied, prevent checkout | |
if ( ! $coupon_applied ) { | |
wc_clear_notices(); // Clear all other notices | |
wc_add_notice( sprintf( 'A coupon code "%s" is required to complete the purchase.', $coupon_code ), 'error' ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment