Shopping Cart

Your cart is currently empty.

Continue browsing here.

Enable cookies to use the shopping cart

Cart Updated
Variant Title has been added to your shopping cart.    View Cart   or   Checkout Now
Variant Title has been removed from your shopping cart.

Using Shopify Scripts to Block Sale Items from Discount Codes


Rebecca Gatesman

As a Shopify Plus customer, you have access to a powerful scripting engine, Shopify Scripts. These small snippets of Ruby allow you to individualize experiences for your customers in their cart and at checkout.

Using the Shopify Scripts API you can write and publish scripts using the Script Editor.

One of our Shopify Plus clients came to us looking for a way to block discounts codes from being used on sale items. This is a perfect instance where Shopify Scripts could provide this customization.

The first step was to find a way to to distinguish when a sale item was in a user's cart. We chose to use a line item property that is passed to the cart whenever the ‘compare at price’ is greater than the item price. This was done by adding this little snippet of code to the product-template.liquid within the product form.

First locate the product form within the template, it's as simple as searching for

See the Pen Scripts: Block Sales &rt; Form by Ross Beyeler (@growthspark) on CodePen.

Within the form we need to determine if a product is on sale. As I stated earlier, we simply used the compare at price and the item price to make this determination. Then within that liquid, we included a hidden input field that would pass a line item property of "saleitem" to the cart.

See the Pen Scripts &rt; Block Discounts > Compare Price by Ross Beyeler (@growthspark) on CodePen.

With that line item property, the cart will have the necessary data for our script to cycle through the items in the cart, determine whether any of those items has a property of "saleitem", and then reject a discount code if a user inputs one.

See the Pen Scripts &rt; Block Sales > Rejection by Ross Beyeler (@growthspark) on CodePen.

First we check to see if a discount code has been entered. Then the script cycles through all the line items in the cart, reads their properties to see if the saleitem property exists, and if so, we'll reject the discount code with the message specified.

You'll notice that the line item property has an underscore prepended to it, this is used to hide the property from the cart and checkout process. If your current theme doesn't support this, you need to simply find this bit of code in your cart.liquid and add the conditional statement directly below the {% for p in %} so that it resembles the code below.

See the Pen Scripts &rt; Block Sales > Loop by Ross Beyeler (@growthspark) on CodePen.

And that's it! When this script is turned on, it will then prevent users from using discount codes on any sale items. If they try, they will be alerted with a message informing them that they cannot use the discount code and no discount will be applied.

Share this

Additional Articles