fbpx
change proceed to checkout text - featured image

How to exclude WooCommerce product from coupons

Are you looking for ways to use your discount codes more efficiently? We’ve got something for you. In this guide, we’ll show you different ways to exclude a WooCommerce product from coupons so discounts don’t apply to them.

Now that Black Friday and Christmas are approaching, many online stores start to offer deals and promotions. When you’re setting up your discount coupons and sharing them online, you want to make sure that they don’t create significant drops in your profits. If your sales increase 25% but the profit per sale decreases 50% you’re better off not offering any discount.

Finding the right balance between more sales and profits per sale isn’t always easy. One of the best ways to do that is to maintain certain products discount-free. You can use certain discounted items like hooks and then try to upsell your customers with other products that don’t have discounts.

In this guide, we’ll show you different ways to exclude WooCommerce products from coupons, so discounts don’t apply to them.

Why Exclude Products From Coupons?

Using discount coupons is a common marketing strategy used by many WooCommerce stores to boost their sales. If used smartly, discounts can be beneficial for online businesses. However, they can also create a drop in revenue if they aren’t applied correctly and the increase in sales doesn’t at least equal the price drop.

Additionally, with larger catalogs, there’s also the possibility of loopholes that your customers might take advantage of. For example, you may not want to apply a discount to a bundled product that already has a discount applied to it. Similarly, you may end up losing money if customers use a discount on products that already have thin margins.

That is why it’s a good option to exclude certain WooCommerce products from discount coupons. This way you can create as many discount coupons and codes as you want while making sure that those specific products won’t be affected by them.

Let’s have a look at how you can do so and what kind of tools you can use to exclude products from discounts.

How to exclude a WooCommerce product from coupons

There are two main ways to exclude WooCommerce products from discount coupons:

  1. With the default WooCommerce options
  2. Programmatically

Let’s check out each option in more detail.

1) Exclude WooCommerce Product From Coupons using Default WooCommerce Options

WooCommerce provides you with the option to disable specific coupons for certain products and categories. To access these options, go to WooCommerce > Coupons on your WordPress Admin Dashboard.

Then, open any of the coupons that you’d like to exclude by clicking Edit under the coupon. Then, scroll down and go to the Usage restriction tab.

exclude woocommerce product from coupons - coupons tab

Here you can use the Exclude Product and Exclude Categories options to exclude certain products from this specific coupon. Simply type in the product name on the Exclude Product field or the product category on the Exclude Categories field. For example, if you don’t want to apply discounts to Product 15 and Product Bundles, you would do something like this.

exclude woocommerce product from coupons - coupon restrictions

Now, this specific coupon won’t apply to those products. However, note that all the other coupons that apply to that item will work unless you repeat this process in the other coupon.

exclude woocommerce product from coupons - excluded product

If you have many coupons, this process can be time-consuming because you would have to go one by one and add the products you want to exclude. The good news is that there’s a more efficient way to do it with a bit of code. Let’s see how you can exclude WooCommerce products from all coupons programmatically.

2) Exclude WooCommerce Product from Coupons Programmatically

You can also exclude a WooCommerce product from discount coupons programmatically. The advantage of this method is that it gives you a lot of flexibility to make sure that discounts don’t apply to certain items. For example, you can disable specific product IDs for coupons or add a checkbox to the WooCommerce Product Data options.

Let’s look at how you can enable some of these options.

NOTE: As we’ll edit some core files, before we start, we recommend you create a backup of your site and install a child theme if you don’t have one already.

2.1) Exclude WooCommerce products from coupons using the Product ID

One of the easiest ways to exclude WooCommerce products from coupons is by simply adding a code snippet that disables coupons from working for a specific product ID.

To do this, you need to find the product ID first. For this, open your WordPress Admin Dashboard, go to Products and you will see the list of all your WooCommerce products. Hover over the product that you don’t want to apply the discounts to and you will see the product ID.

exclude woocommerce product from coupons - product ID

Write it down and leave it somewhere handy because you will need it for the following step.

After that, open your Theme functions file by heading to Appearance > Theme Editor. Then, use the right Theme Files sidebar and open the functions.php file.

exclude woocommerce products from coupons - theme editor functions

Add the following code snippet to the file but remember to replace the PRODUCT_ID with the corresponding ID of the product you want to exclude.

add_filter( 'woocommerce_coupon_is_valid_for_product', 'quadlayers_exclude_product_from_product_promotions', 9999, 4 );
function quadlayers_exclude_product_from_product_promotions( $valid, $product, $coupon, $values ) {
// REPLACE THE PRODUCT ID (E.G. 145)
if ( PRODUCT_ID == $product->get_id() ) {
$valid = false;
}
return $valid;
}

The code excludes from discounts the product IDs that we specify. For example, in our case, the product ID is 145, so the code snippet will be:

add_filter( 'woocommerce_coupon_is_valid_for_product', 'quadlayers_exclude_product_from_product_promotions', 9999, 4 );
function quadlayers_exclude_product_from_product_promotions( $valid, $product, $coupon, $values ) {
// PRODUCT ID = 145
if ( 145 == $product->get_id() ) {
$valid = false;
}
return $valid;
}

That’s it! The product you’ve specified will be excluded from all your coupons. For more information about this, check out this site.

2.2) Exclude WooCommerce Products on Sale from discount coupons

Another interesting option is to exclude products that already have a discount applied from coupon codes. For example, if you have products that are already on sale, you can add a script to make sure that they don’t get an extra discount. This is very useful when you have a sales promotion ongoing in your store and you want to ensure that your discounts aren’t applied to any of the products that are on sale.

The process for this is similar to the one above. Simply go to Appearance > Theme Editor on your admin dashboard and open the functions.php file on the right theme files sidebar. Then, paste the following script to exclude from discounts all the products that are already on sale.

add_filter('xa_pbu_skip_product_on_sale','ql_skip_product_on_sale_from_discount',1,2);
function ql_skip_product_on_sale_from_discount($return_val,$pid) { 
$sale_price = get_post_meta( $pid,'_sale_price',true ); 
if( !empty($sale_price) ) {
return true; // exclude this product if true
}
return $return_val;
}

exclude woocommerce product from coupons - excluded product on sales

Alternatively, you can use the following code to exclude certain products that are already on sale from coupons. Simply replace the product ID with your ID.

add_filter('xa_pbu_skip_product','ql_skip_product_from_discount',1,2);
function ql_skip_product_from_discount($return_val,$pid) {
$pid_to_skip = array(PRODUCT ID);   // Product IDs to exclude
if( in_array($pid,$pid_to_skip)) {
return true;   // exclude this product if true
}
return $return_val;
}

If you want to add multiple products, simply add as many as you want and separate them with a comma. You can find more information about that here.

Adding an Option to Exclude a WooCommerce Product From Coupons Programmatically

If you think that this is something that you may do often, doing the above process every time might not be the most comfortable thing. In that case, there’s a more efficient option. You can add a custom function that adds a checkbox to the backend and allows you to disable coupons individually for each product.

You can simply add this snippet to all your items and later decide which WooCommerce products you want to exclude from coupons and when. This is an interesting option because allows you to make that option dynamic by selecting or deselecting that checkbox instead of having to add and remove the script from the functions.php file.

To do that, on your admin dashboard go to Appearance > Theme Editor and open the functions.php file on the Theme Files sidebar.

exclude woocommerce products from coupons - theme editor functions

Now go ahead and paste this code into the editor:

// Create a custom field in the Product general setting tab
add_action( 'woocommerce_product_options_general_product_data', 'ql_add_custom_field_general_product_fields' );
function ql_add_custom_field_general_product_fields(){
global $post;

echo '<div class="product_custom_field">';

// Custom Product Checkbox Field
woocommerce_wp_checkbox( array(
'id' => '_disabled_for_coupons',
'label' => __('Disabled for coupons', 'woocommerce'),
'description' => __('Disable this product from coupon discounts', 'woocommerce'),
'desc_tip' => 'true',
) );

echo '</div>';;
}

// Save the custom field and update all the excluded product ids in WP settings
add_action( 'woocommerce_process_product_meta', 'ql_save_custom_field_general_product_fields', 10, 1 );
function ql_save_custom_field_general_product_fields( $post_id ){

$current_disabled = isset( $_POST['_disabled_for_coupons'] ) ? 'yes' : 'no';

$disabled_products = get_option( '_products_disabled_for_coupons' );
if( empty($disabled_products) ) {
if( $current_disabled == 'yes' )
$disabled_products = array( $post_id );
} else {
if( $current_disabled == 'yes' ) {
$disabled_products[] = $post_id;
$disabled_products = array_unique( $disabled_products );
} else {
if ( ( $key = array_search( $post_id, $disabled_products ) ) !== false )
unset( $disabled_products[$key] );
}
}

update_post_meta( $post_id, '_disabled_for_coupons', $current_disabled );
update_option( '_products_disabled_for_coupons', $disabled_products );
}

// Make coupons invalid at product level
add_filter('woocommerce_coupon_is_valid_for_product', 'set_coupon_validity_for_excluded_products', 12, 4);
function set_coupon_validity_for_excluded_products($valid, $product, $coupon, $values ){
if( ! count(get_option( '_products_disabled_for_coupons' )) > 0 ) return $valid;

$disabled_products = get_option( '_products_disabled_for_coupons' );
if( in_array( $product->get_id(), $disabled_products ) )
$valid = false;

return $valid;
}

// Set the product discount amount to zero
add_filter( 'woocommerce_coupon_get_discount_amount', 'zero_discount_for_excluded_products', 12, 5 );
function zero_discount_for_excluded_products($discount, $discounting_amount, $cart_item, $single, $coupon ){
if( ! count(get_option( '_products_disabled_for_coupons' )) > 0 ) return $discount;

$disabled_products = get_option( '_products_disabled_for_coupons' );
if( in_array( $cart_item['product_id'], $disabled_products ) )
$discount = 0;

return $discount;
}

exclude woocommerce product from coupons - excluded products stack

After that, update the file. Now let’s see how to use that custom checkbox you’ve just added.

Using the Function and the Disable Coupons Option

Go to the Products section and open any product that you’d like to exclude from coupons. On the General Tab of the Product data, you should now see a new option called Disabled for coupons.

Enabling this option will exclude this WooCommerce product from discount coupons. If you then uncheck it, discounts will apply to this item again. Remember to save the changes and check the front end to make sure everything works as expected.

Conclusion

All in all, excluding certain items from discounts is a good alternative to maximize revenue even when you offer deals. You may want to apply discounts to certain products but not to others either because your margins are already quite thin or they already have a discount applied for example.

In this guide, we’ve seen different ways to skip and exclude a WooCommerce product from coupons:

  • Using the WooCommerce default options
  • Programmatically excluding individual products, all the products, or only items on sale using the product IDs
  • Adding a custom script to include an option in the backend to disable coupons for specific products

If you have any questions regarding any of these steps, let us know in the comments section below and we’ll do our best to help you.

Finally, if you’re looking for additional ways to set up discounts and the best ways to use them, we recommend you check out the following guides:

Hello!

Click one of our representatives below to chat on Telegram or send us an email to [email protected]

How can I help you?