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:

and this inside the loop (so just below {% assign hasDiscountedProduct = false %} ):

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:

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:

with:

(make sure to hide this tag on the frontend if you display tags on your site)