menu

Shopify – Auto fill discount code on checkout page

Locate this line of code in your cart.liquid template

<form action="/cart" method="post" id="cart">

Directly below that line of code you need to place something like this

<input class="js-form-discount" type="hidden" name="discount" value="RESELLERDISCOUNTCODE" >

Where “RESELLERDISCOUNTCODE” is your discount code.

If you want apply this to only a specific group of customers. you can use tags to create a customer group and then only apply the code to customers who are in that group.

{% if customer.tags contains 'RESELLER' %}

   <input type="hidden" name="discount" value="RESELLERDISCOUNTCODE" >
  
 {% endif %}

Add coupon code from URL

This will allow you to add a coupon code to any URL and auto populate that coupon code at checkout.

Example URL – https://myshopifystore.com?coupon=MYCOUPONCODE
you can add coupon=MYCOUPONCODE to any URL on your store

Locate this line of code in your cart.liquid template

<form action="/cart" method="post" id="cart">

Directly below that line of code you need to place something like this

<input class="js-form-discount" type="hidden" name="discount" value="RESELLERDISCOUNTCODE" >

Make sure you are including jquery and Jquery cookie in your theme. Your theme probably already has jquery so you would just need to include jquery-cookie

Jquery: https://jquery.com/
jquery-cookie: https://github.com/carhartl/jquery-cookie

This Gist should be a solid building block to customize for your specific needs and should be added at the end of your layout.theme file before your tag and within a script tag

<script type="text/javascript">

29 Responses to “Shopify – Auto fill discount code on checkout page”

  1. roberto says:

    if i want to do a discount based on the collection name how can i do that?

    • TMBR says:

      Shopify lets you set up discounts by collections

      and then you would just use the name of the discount code “Collection Discount” as the hidden field value.

  2. Stephen says:

    Thanks for your post. This enabled me to add an auto discount to the cart page.
    But this does not work when clicking the checkout button in the drop down drawer.
    How can you add the code to automatically fire on this button?

    Thanks

  3. Ramon says:

    Hello,

    just wanted to say thanks, I’ve successfully used this on my shop to automatically apply a discount when a certain value is reached, in my case $30.

    Here is the code I’ve used:

    {% if cart.total_price >= 3000 %}

    {% endif %}

    It’s important to notice that $30 translate to 3000 in the code. So for example $29.95 would be 2995.

    I’ve used this in conjunction with the “Free Shipping Bar by Hextom” to remind the customer about the additional amount they should expend to receive the discount code.

    Again thanks!

  4. Dave says:

    How would you use this method based on a product that is included in the cart with a specific tag?

    tried this code:

    {% if product.tags contains ‘PROCLUB’ %}

    {% endif %}

    but didn’t work the same as customer group example

    • TMBR says:

      Hey Dave – you would have to iterate through each product in your cart and then put a true false flag in that loop

      {% assign autodiscount = false %}

      {% for item in cart.items %}
      {% if item.product.tags contains ‘PROCLUB’ %}
      {% assign autodiscount = true %}
      {% endif %}
      {% endfor %}

      {% if autodiscount = true %}

      {% endif %}

      Keep in mind that this would get set to true if only one product in the cart had tag ‘PROCLUB’ – the loop would need some additional checks if it were to only be set to true if all products have tag ‘PROCLUB’

  5. Seb. Garcia says:

    Thanks for the tip!

    Is that me or do we not have to click “APPLY” anymore for the code to be taken into account?! It seems like now the cart does that automatically or something.

  6. mike says:

    Hello,

    what if instead of customer tag, it comes from a referral link or URL?

  7. Scott Banner says:

    I just can not get the customer tag to trigger any response in my cart. I assigned my own customer account a tag named “IGPR”. In cart.iquid, I’ve added the following…
    `{% if customer.tags contains ‘IGPR’ %}
    WORKS!
    {% else %}
    Leave a note below
    {% endif %}`
    I signed in and added a product to the cart and all I get to display is “Leave a note below”. What am I missing?

    • TMBR says:

      The code above should work if the customer you are logged in with is tagged ‘IGPR’ is may be case sensitive to so make sure your tag is all caps.

  8. Nick says:

    Is there a way to restrict a discount from being used if you belong to a tag customer group? That would save the day to prevent wholesale people from getting discounts on wholesale prices.

    • TMBR says:

      I have used tags to create customer groups / saved searches. Go to customers > filter customers > tagged with > “your wholesale tag” and then save search as Wholesale. You can do the same with retail customers and then when you are creating a coupon code you choose “customers in group” where you would pick Retail / Wholesale etc… then only customers in those groups can use the discount code.

  9. […] Shopify – Auto fill discount code on checkout page […]

  10. Anne says:

    Thank you!!!!! this was a life saver.

  11. Faruq says:

    Thank you for your post. Is there a way to set up automatic discount based on cart quantity?

  12. Navjeet Singh says:

    This will work if product contains only define tag

    {% for item in cart.items %}
    {% if item.product.tags contains ‘BOGO’ %}

    {% endif %}
    {% endfor %}

  13. Hi I can’t make it work, where and how is the jquery and Jquery cookie include in the theme? I put code in cart.liquid template and layout.theme the script, yet nothing happens, cannot find what is missing. Any help?

  14. Alexa Galtman says:

    Is there a way i can make this work as an either or senario with 2 coupon codes. 1 would give 10% off if one item was purchased and and the second is 30% off if you get 2 or more

  15. vishnu pratap singh says:

    Hi,
    Thank you for such a great information.
    I have a query regarding this.
    How it will work if i want to give this with registration form.

  16. Jon Folse says:

    If a customer clicks on the CHECKOUT button for an item vs them putting it in their cart and then checking out, it doesn’t apply that discount. How do we rectify?

  17. vikash says:

    How to apply a code based on the URL they are coming from not on the URL they are landing. so I can have different discount codes auto-applied based on the landing page they are coming from

  18. Alexa says:

    Hi i’ve used this method with 3 different shopify themes to great success. However it is not working with the narrative theme. any ideas?

  19. gelsas says:

    Hi,

    I want to achieve the following:
    What I am trying to achieve is:
    Get current customer cart Item count
    If item count 2 set discount cookie to 25OFF
    If item count 3 set discount cookie to 30OFF

    Can I do that with your code, if yes how ?

  20. Thanks for this guidance!

Leave a Reply

Your email address will not be published. Required fields are marked *

  • *If you have one
  • Do you have any specific goals or objectives lined out?
  • This field is for validation purposes and should be left unchanged.

Questions ?

[email protected] - 307.200.0087