Shopify automatic discount for product in cart
In shopify there is no out of the box way to add a discount code when a product get’s added to the cart.
If you have a certain product that you want to add a discount for when it is added to the cart, you can add a hidden input field with your discountcode like this:
comments added for clarity.
in cart-template.liquid Add this just before the “{% for item in cart.items %}” part:
{% assign hasDiscountedProduct = false %}
and this inside the loop (so just below {% assign hasDiscountedProduct = false %} ):
{% comment %} see if we have the discounted product in the cart If they need to have 2 different products, you need some extra work (maybe set 2 vars, then after the loop check if both vars are true before adding the hidden field {% endcomment %} {% if item.product_id == 10056422096932 %} {% assign hasDiscountedProduct = true %} {% endif %}
You can add some custom content inside the forloop on the discounted product, or just below the endfor, so the customer will see they will get the discount.
Add this snippet just before the form closing tag, so the input get’s posted to the checkout part:
{% comment %} Is a product in the cart valid for using a discount code? let's add the discount code field{% endcomment %} {% if hasDiscountedProduct == true %} <input type="hidden" name="discount" value="your-discount-code-created-in-shopify" /> {% endif %}
If you have a lot of products where you want the discount to be automatically added to the checkout, you could add a specific tag to these products like “discounted_product”
and then replace this code:
{% if item.product_id == 10056422096932 %}
with:
{% if item.product.tags contains 'discounted_product' %}
(make sure to hide this tag on the frontend if you display tags on your site)