Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

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

How to Hide the WooCommerce Coupon code field based on specific products | tychesoftwares.com

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 –

add_filter('woocommerce_coupons_enabled', 'ts_hide_coupon_field_on_cart', 10, 1);

function ts_hide_coupon_field_on_cart($enabled) {
    // Ensure the WC()->cart object is available
    if ( is_object(WC()->cart) ) {
        $product_id = 55;
        $cart = WC()->cart->get_cart();

        foreach ($cart as $cart_item_key => $cart_item) {
            if ($cart_item['data']->get_id() == $product_id) {
                return false;
            }
        }
    }

    return $enabled;
}

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:

add_filter('woocommerce_coupons_enabled', 'ts_hide_coupon_field_on_cart', 10, 1);

function ts_hide_coupon_field_on_cart($enabled) {
    // Ensure the WC()->cart object is available
    if (is_object(WC()->cart)) {
        $product_ids = array(8, 55, 57);
        $cart = WC()->cart->get_cart();

        foreach ($cart as $cart_item_key => $cart_item) {
            if (in_array($cart_item['data']->get_id(), $product_ids)) {
                return false;
            }
        }
    }

    return $enabled;
}

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:

add_filter('woocommerce_coupons_enabled', 'ts_hide_coupon_field_on_cart', 10, 1);

function ts_hide_coupon_field_on_cart($enabled) {
    // Ensure the WC()->cart object is available
    if (is_object(WC()->cart)) {
        $product_ids = array(8, 55,57);
        $count = 0;
        $cart = WC()->cart->get_cart();

        foreach ($cart as $cart_item_key => $cart_item) {
            if (in_array($cart_item['data']->get_id(), $product_ids)) {
                $count++;
            }
        }

        if ($count == count($product_ids)) {
            $enabled = false;
        }
    }

    return $enabled;
}

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:

add_filter('woocommerce_coupons_enabled', 'ts_hide_coupon_field_on_cart', 10, 1);

function ts_hide_coupon_field_on_cart($enabled) {
    // Ensure the WC()->cart object is available
    if (is_object(WC()->cart)) {
        $product_categories = array('hd', 'led');
        $cart = WC()->cart->get_cart();

        foreach ($cart as $cart_item_key => $cart_item) {
            $_product = wc_get_product($cart_item['data']->get_id());

            foreach ($product_categories as $category) {
                if (has_term($category, 'product_cat', $cart_item['data']->get_id())) {
                    return false;
                }
            }
        }
    }

    return $enabled;
}

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: WooCommerce How Tos, Code Snippets, WooCommerce Tutorials

Share It:

Subscribe
Notify of
13 Comments
Newest
Oldest
Inline Feedbacks
View all comments
Claudio
2 months ago

Hello, I don’t see the code snippet in this article. I want to hide the coupon code field for multiple product, can you give me the code? Thank you.

2 months ago
Reply to  Claudio

Hi Claudio,
The post has been updated with the code snippets that were missing. Please check it.

Pam Blizzard
2 years ago

This code doesn’t work for me, with nothing but WooCommerce enabled. Would love to see an update, or a plugin that hides the coupon field based on product.

Admin
2 years ago
Reply to  Pam Blizzard

Hi Pam, I just verified all the code snippets provided in this post and all worked fine for me. I am assuming that you are trying to use the code given in the 1st scenario. If that is the case, then please check if you have replaced the correct product id in the code snippet. https://prnt.sc/26g9lws If still, it doesn’t work for you then kindly share the admin level access details of your website and FTP access details to make changes in the file so that we debug it on your site. You can share these details on below email… Read more »

Last edited 2 years ago by Kartik Parmar
Jasper
2 years ago
Reply to  Pam Blizzard

Hey,

as those codes were also not working for me i found a workaround by hiding it depending on cart total. Maybe it will also work for you.
For example this will hide the coupon field if the value of the cart in total is less than 10:

add_filter( 'woocommerce_coupons_enabled','l2_hide_coupon_field_on_cart' );
function l2_hide_coupon_field_on_cart( $enabled ) {
	global $woocommerce;
	if( $woocommerce->cart->cart_contents_total < 10 ) return false;
	return $enabled;
}
Jono
2 years ago

Thank you, this solved my issue! Worked perfectly, now I’ve got the coupon field hidden only for one product.

2 years ago

Getting Error on All the Above Methods. I have tried all the above codes but its showing the same error.
Your PHP code changes were rolled back due to an error on line 229 of file wp-content/themes/konte-child/functions.php. Please fix and try saving again.

Uncaught Error: Call to a member function get_cart() on null in wp-content/themes/konte-child/functions.php:229
Please Help.

patrick clausen
3 years ago

Hi I want this but the other way around I want to disable coupons on all product but 1

13
0
Would love your thoughts, please comment.x
()
x