Check if Certain Discounts Have Been Applied at Checkout

I’m trying to create a Script in Checkout.liquid file that checks if a certain coupon was added to the checkout then hide the coupon code section so customers can’t remove the checkout code. The script is not working so any assistance will be much appreciated. Thanks!

{% for item in checkout.line_items %}
	{% if discount_application.title == 'my_discount_code' %}
		  
	{% endif %}
{% endfor %}

@abelvf Any luck figuring this out? I too am try to check if certain discounts use entered.

As far as I can tell discounts activated by a code don’t show up on the cart prior to the checkout page. I’ve checked both cart.json and all the liquid objects but no code-based discounts show in either, even if the code has been applied prior to checkout. Scripts and Automatic discounts work as expected.

I haven’t found any documentation on why this may be or if there are conditions that affect how code-based discounts show. If anybody has any further insight let me know seems like a big miss from the shopify side.

I don’t know if you’re still trying to figure this out, but I solved it while trying to get a line-item discount.

Here I did it for item.discounts;

{% for discount in item.discounts %}      
     {% if discount.code contains '[discount code title here]' %}

          some code here

    {% endif %}
{% endfor %}

But you could wrap that in a cart.items for loop;

{% for item in cart.items %}
    {% for discount in item.discounts %}
        {% if discount.code contains '[discount title here]' %}

            some code here

        {% endif %}
    {% endfor %}
{% endfor %}

Hope it works as well for you as it did for me.

Did you get this working for code-based discounts as well? it works great for me for Shopify automatic discounts and Line Item scripts, but not discounts activated via a discount code.

Hey guys,

I was trying to find a way to inform the frontend (JS) if a discount is applied by the customer using a discount code link. I’m seeing that there is a session cookie set called discount_code that we should be able to use for this purpose.

Have a great one!

2 Likes

Thank you so much. I have a same problem and your answer help me so much

This works great initially, but on a page refresh or page change, the discount_code cookie is removed, so any checks you had on the front end to show a customer that they have a discount applied no longer work. Is there another way around this? There has to be another way to check if a cookie has been applied rather than the discount_code cookie. Is there anything set in the LocalStorage?