Discount code on cart not applying

DEV-ONI
Shopify Partner
5 0 0

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

 

Input element:

 

<input type="text" name="discount" value="<SOME_DISCOUNT_CODE>" >

 


OR action:

 

 

<form id="cart_form" action="/cart?locale=en&discount=<SOME_DISCOUNT_CODE>" method="post" novalidate="">
</form>

 



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

Replies 2 (2)

BSS-Commerce
Shopify Expert
3477 461 516

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

 

In the theme. liquid file, add the following code just after the <head> 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. 

If our suggestions are useful, please let us know by giving it a like or marking it as a solution.

For B2B solutions and custom pricing, check out B2B Solution & Custom Pricing |
For adding product labels/badges, visit Product Labels by BSS

BSS Commerce - Shopify Apps & Store Development Service Provider

Hire a Shopify expert | Shopify Plus Store Development

MikeDe
Tourist
11 0 8

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.