Is It Possible To Send Discount Code From Cart Page To Checkout Page In Shopify?

- 1 answer

How we can send discount code from cart page to checkout page? I am trying to add discount code on cart page in hidden field and want to use it on checkout page automatically. I also don't want to enter discount code on checkout page manually.

I have discount code on cart page and I am trying to put it into hidden filed with the help of javascript. following is my code:

    type: "GET",        
    url: url,
    data: data,
    contentType: "application/json",
    headers: {  'Access-Control-Allow-Origin': '*' },
    success: function(data) {
    var dcv = data.discountCode;
    document.getElementById("quantityDiscount").value = 'dcv';

from above Ajax call, I received discount code in my dcv variable and put it on input field having #quantityDiscount as id. following is my input filed where I am trying to save this discount code.

<input  id="quantityDiscount" class="js-form-discount" type="hidden" name="discount" value="" >

Please look at this issue and help me to solve it.

Thanks in advance!



for sending it to checkout page

localStorage.setItem("discount", dcv);
// Retrieve

console.log(localStorage.getItem("discount"))   // your discount value
sessionStorage.setItem("discount", dcv);
// Retrieve

console.log(sessionStorage.getItem("discount"))   // your discount value
  • The localStorage and sessionStorage properties allow to save key/value pairs in a web browser.

  • The sessionStorage object stores data for only one session (the data is deleted when the browser tab is closed).