Why isn't my conditional discount code applying in the cart?

We are attempting to apply discount codes conditionally using Javascript on the cart form,
but neither of these methods seem to work

Input element:


OR action:


The discount is not reflected on checkout.
This used to work? Did anything on Shopify’s end change?

Hi @DEV-ONI , you can try this way:

In the theme. liquid file, add the following code just after the tag:

{% if template.name == 'index' %} <script> (function(window) { if (URL) { var url = new URL(window.location.href); var discount = url.searchParams.get('discount'); if (typeof(discount) !== 'undefined' && discount !== null && discount.length > 0) { sessionStorage.setItem('discount', discount); window.location.href = '/discount/' + discount; } } })(window); </script> {% endif %}

 

In the file cart-template. liquid add the input tag:

<input id="input-discount" type="text" name="discount" placeholder="optional"> 

And then add the following code to the bottom of the file:

{% javascript %}
$(document).ready(function() {
    var discount = sessionStorage.getItem('discount');
    if (discount !== null) {
        var el = $('#input-discount');
        el.prop('readonly', true);
        el.val(discount);
    }
});
{% endjavascript %}

Let me know if this helps you.

Hi,

I also noticed this issue. It seems that it started happening about a month ago.
When you try to pass the discount code with the “discount” GET parameter in the URL, the discount isn’t passed anymore to the Shopfiy Checkout.
It seems that this issue happens on some stores with the “Checkout One” version, but it seems to be working in the “Checkout Classic” version.

Also, from what I see, it seems that when you pass the discount code, the Shopify Checkout writes it into the cookie, but then immediately after that, it removes it from the cookie, hence why the issue happens.