Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shameemreza/90f10bb6aa3d6b837f934e9b4821cee8 to your computer and use it in GitHub Desktop.
Save shameemreza/90f10bb6aa3d6b837f934e9b4821cee8 to your computer and use it in GitHub Desktop.
Make mandatory coupons for all products in WooCommerce
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