Search the site:

Home Blog How to Hide the WooCommerce Coupon code field based on specific products

How to Hide the WooCommerce Coupon code field based on specific products

In the post How to Hide the WooCommerce Coupon code field, we explained how we can hide the coupon code field from the WooCommerce cart & checkout page. However, the coupon code field will be hidden all the time using that method.

One of our customers who also uses our Abandoned Cart Pro plugin, asked this question on a Facebook group as to how can the coupon code field be hidden only for certain products. Her use case was that she needed to remove that option for certain low priced products only. This would also help reduce cart abandonment.

In this post, I will explain how we can hide the coupon code field only for specific products. If these specific products are added in the cart, then the “‘Have a coupon? Click here to enter your code’ ” block will not be displayed on the WooCommerce cart and checkout page.

We will look at 4 ways in which you can hide or remove the coupon code field based on products or categories.

1. Hide the coupon field for any one product:

Suppose, I want to hide the coupon code field if a product called ‘LED TV’ is present in the cart. I don’t want people to apply coupon if they are buying this product. Let’s see how we can do that –

The product ID is 55. In the above example, we will check all the items in the cart. If the ID of any item matches the product ID i.e 55 , we will return false i.e hide the field.

hide coupons on cart and checkout for specific products based on ids

As you can see, the coupon code is not displayed on the cart & checkout page.

2. Hide the coupon field for multiple products:

In the above example, we check for only one product. What if we want to apply this to multiple products?  If any of the specified products is present in the cart, then we will hide the coupon code field.

Here is the code snippet:

Here, we have defined an array of product IDs. We loop through the cart items and check if the ID of the item is present in our array. If yes, then we hide the coupon field without checking further.

This method will also work for a single product. You will just have to pass one product ID in the array.

3. Hide the coupon code field if all specific products are present in the cart:

Now, suppose if we want to hide the coupon code field for 2 specific products i.e we want to hide the field only when both the products are present in the cart.

Here is the code snippet:

Only if both the products are present in the cart, then the coupon code field will be hidden –

hide coupons on cart and checkout for specific products based on ids

If only 1 of those products is present in the cart, then the coupon code field will show up on the cart & checkout page –

hide coupons on cart and checkout for specific products based on ids

4. Hide the coupon code field based on product categories (by slug):

Let’s take another example where we can hide the coupon code field if the product of certain categories are present in the cart. For example, if products of category ‘HD’ or ‘LED TVs’ are present in the cart, we will hide the coupon code field.

Here we will check for the categories using their slug. Here is the code snippet:

We check each item from the cart and if any item belongs to one of the categories from our $product_categories array, then we return false and the coupon code field is not displayed on the cart and checkout page. Here we have used a function called has_term which will check if that product has a specific category or not.

Here is a screenshot of checkout page when a product of the category ‘LED TVs’ is added to the cart –

hide coupons on cart and checkout for specific products based on categories

 

And when we remove the product ‘LED TV’ from the cart, the coupon field will be displayed once again –

hide coupons on cart and checkout for specific products

 

There are many ways in which you can hide the coupon codes. You can also hide the fields based on product tags as we did with the categories. Another idea is to hide the coupon field based on the product prices.

Try these methods and let us know how it works. If you have some other ideas, please share them with us. We are always eager to hear what our readers have to say.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of
from purchased

Subscribe Now