All Collections
Shopify Subscription App
Customizing PayWhirl on Shopify
How to automatically apply a Shopify discount code to a subscription in the cart
How to automatically apply a Shopify discount code to a subscription in the cart

Automatically apply a discount code to any checkout in Shopify. This requires coding and may require the help of a developer/theme expert.

Larry Brager avatar
Written by Larry Brager
Updated over a week ago

Shopify doesn't yet support automatic discount codes when subscription products are present in the cart or checkout. However, if you are familiar with HTML, you can work around this limitation by making some changes to your cart.liquid file in your theme. This is an advanced tutorial and may require a developer.

Note: This guide assumes you have a general knowledge of coding and Shopify liquid. If you need development help, we can recommend developers who can help out for a fee.

Say you want to automatically apply a discount to give customers a free trial or make shipping free for a subscription product. A traditional Shopify discount code would work, but the user must enter it at checkout. This guide makes the application automatic, so the user doesn't have to do anything but view their cart and proceed to checkout. It works with product tags, so you can target only the products you want.

For this tutorial, we will use the discount code "Sale" and assume you already have the discount code created in your Shopify account. It also needs to be configured to apply to subscription products in the Shopify discount code settings. You can learn more about creating Shopify discount codes here.

Start off by editing your theme and going to your cart.liquid file. If your cart.liquid is basically empty, you might need to work in your cart-template.liquid (or whatever section is referenced in your theme's cart.liquid file)
​

Next, find the form and add the following line of code right below the opening form element.
​

{% assign autodiscount = false %}

Continuing, find the for loop that iterates through the items in the cart and add the following code right below it. This code will check for a tag called "discount" on each product in your cart and set a variable called autodiscount to true if it's present. Note: you can change this to another tag, but in our example, we'll simply use discount as our product tag. We'll add it to the product(s) to which we want the discount to apply automatically later in the tutorial.
​

 {% if item.product.tags contains 'discount' %} 
{% assign autodiscount = true %}
{% endif %}

Now, find the end of the for loop and paste the following code below the closing {% endfor %} liquid element in your theme file. This code will add a hidden input field to your cart form with a name set to "discount" (our tag) and a value of "Sale" (our discount code).
​

{% if autodiscount == true %}
<input type="hidden" name="discount" value="Sale" >
{% endif %}

Finally, we need to ensure that all the products we want to be discounted automatically have a tag of "discount" in Shopify on the product page. When customers add a product tagged with discount to their cart, the discount code "Sale" will be added automatically and be visible in the cart or checkout, depending on your theme.

This is a limited example for demonstration purposes and may require more development/customization to ensure it works as expected. Please let us know if you have any questions.

Related Articles:

Sincerely,
The PayWhirl Team

Did this answer your question?