Re: Exclude sale items from discount codes on EXISTING discounts

Exclude sale items from discount codes on EXISTING discounts

EyefourDesign
Tourist
6 0 0

Hello All!

So I have an issue. My client has a store where they have 100's of existing % off discount codes for affiliates. They recently started putting items on sale using the compare at pricing and do not want those items to be discounted. I know how to exclude items from NEW codes but how do I do it with existing codes?

Now if it were a few codes, certainly I could just go in and edit or remake them, but there are 100's.

I even asked chatgpt and it said for sure that I could add this code to my theme but that didn't work as it promised. Any ideas out there?

document.addEventListener('DOMContentLoaded', function() {
var excludedTags = ['no-discount'];

fetch('/cart.js')
.then(response => response.json())
.then(cart => {
var cartItems = cart.items;

var hasExcludedProduct = cartItems.some(function(item) {
return excludedTags.some(function(tag) {
return item.tags.includes(tag);
});
});

if (hasExcludedProduct) {
var discountCodeInput = document.querySelector('[name="discount"]');
if (discountCodeInput) {
discountCodeInput.disabled = true;

var message = document.createElement('p');
message.style.color = 'red';
message.innerText = 'Discount codes are not applicable on some items in your cart.';
discountCodeInput.parentElement.appendChild(message);
}
}
})
.catch(error => {
console.error('Error fetching the cart:', error);
});
});

Reply 1 (1)

tobebuilds
Shopify Partner
430 29 115

Hi EyefourDesign,

 

Ignore the code ChatGPT gave you... This issue has nothing to do with theme code. ChatGPT tends to make up answers when it doesn't know what it's talking about, which can be highly misleading.

 

Simply put, you need to modify your existing discount codes to only apply to a group of products that excludes anything with a compare at price greater than its sale price.

 

You can either do this by using "Specific products" and adding products one by one or by creating a collection that contains all products that aren't already on sale.

 

I strongly recommend against doing this manually; instead:

  1. Write a script that uses the Shopify API to add a tag to every product where there is either no compare at price, or where the compare at price is less than or equal to the sale price.
  2. Create an automated collection in Shopify that includes only products with that tag.
  3. Write a script that updates all your existing discount codes to only apply to that collection. You can use discountCodeBasicUpdate for this: https://shopify.dev/docs/api/admin-graphql/2024-04/mutations/discountCodeBasicUpdate

 

A common source of confusion in Shopify is that there are 2 totally different ways to offer price reductions for your customers:

  1. Sale price/compare at price
  2. Discounts

As you've seen firsthand, this also leads to problems when you have many existing discounts.

 

If you're looking for a way to exclude sale items from discounts you create in the future as well, you can try our app, Regios Automatic Discounts (4.9 stars, Built for Shopify).

 

tobebuilds_0-1722570876608.png

 

Here's a screenshot of how you could set your discount up:

 

Our app has a setting called "How to handle items already on sale." If you set this to "Don't apply discount to items already on sale" (this is the default), then product variants with a compare at price greater than their sale price will not receive a discount.

 

This way, you can prevent giving customers double discounts.

 

If you have any questions about this, just reply, and I'll be happy to help.

 

Best,

Tobe

 

Founder, Regios Discounts app (4.9 stars)
- Custom discounts made simple
- "Just about any discount you'll ever need"
- Built by an ex-Google software engineer