How to make Coupon Mandatory for WooCommerce Product?

Who doesn’t like discounts? Offering discounts for specific products, a tactic used for decades in physical shops, will increase the interest of your customers to your products.

Discounts in an online shop is usually done with the use of coupons. WooCommerce is quite flexible and gives you a variety of ways to apply coupons to your products. Some are easily done by just checking one or two options on the dashboard, but some might be a bit more involved.

In this article, we will discuss how to make a product available for sale only if the customer has a coupon for it. This is a great marketing tactic in a store that has special offers and discount for members, as the product will be visible to all users, but only those that have a coupon code will be able to buy it. If your product is great, who won’t consider to apply for an membership to your store, so that they can benefit from your exclusive offers?

Getting ready

Before doing any code modifications, it’s always a good idea to take a full backup of our site and make sure that we have FTP access or access to cPanel or Plesk or other control panel with access to a file manager.

If you wonder what is the best way to add code snippets to your site, have a look at our previous article about safely adding PHP code

Don’t forget to activate the use of coupons in your dashboard, by going to WooCommerce -> Settings -> General and checking the “Enable coupons” option.

Require coupon for single product

If you have a single product that you want it to only be available with the use of coupon, all you need is to know the product ID and the coupon code, and modify the following snippet to fit your needs:

add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_for_specific_items' );
function mandatory_coupon_for_specific_items() {
$targeted_ids = array(37); // The targeted product ids (in this array)
$coupon_code = 'summer2'; // The required coupon code
$coupon_applied = in_array( strtolower($coupon_code), WC()->cart->get_applied_coupons() );
// Loop through cart items
foreach(WC()->cart->get_cart() as $cart_item ) {
// Check cart item for defined product Ids and applied coupon
if( in_array( $cart_item['product_id'], $targeted_ids ) && ! $coupon_applied ) {
wc_clear_notices(); // Clear all other notices
// Avoid checkout displaying an error notice
wc_add_notice( sprintf( 'The product"%s" requires a coupon for checkout.', $cart_item['data']->get_name() ), 'error' );
break; // stop the loop
}
}
}

Where:

  • $targeted_ids = array(37): Replace 37 with the ID of your product. You can add multiple products by separating the product IDs with a comma, ex array(37, 12, 99);
  • $coupon_code= 'summer2';: Replace summer2 with your coupon code.

Screenshot of woocommerce checkout when trying to add a product that requires coupon

Require coupon for all products in a specific category

If you want to require a coupon for all the products in a specific category, we can edit the above snippet as shown below:

add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_code' );
function mandatory_coupon_code() {
$product_categories = array( 'clothing' ); // Category ID or slug of targeted category
$coupon_code = 'summer2'; // The required coupon code
$coupon_applied = in_array( strtolower($coupon_code), WC()->cart->get_applied_coupons() );
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ){
if( has_term( $product_categories, 'product_cat', $cart_item['product_id'] ) && ! $coupon_applied ) {
wc_clear_notices(); // Clear all other notices
// Avoid checkout displaying an error notice
wc_add_notice( sprintf( 'The product"%s" requires a coupon for checkout.', $cart_item['data']->get_name() ), 'error' );
break; // stop the loop
}
}
}

Where:

  • $product_categories = array( 'clothing' );: Replace clothing with the slug, or the ID of the targeted category

Wrapping up

Discounts are exciting, and rewarding members and loyal customers with exclusive offers is even more exiting! The code snippets presented in this article will enable you to:

  1. Require coupon for the sale of single product
  2. Require coupon for any product from a specific category.

Leave a Reply