3
votes

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:

$.ajax({
    type: "GET",        
    url: url,
    data: data,
    contentType: "application/json",
    headers: {  'Access-Control-Allow-Origin': '*' },
    success: function(data) {
    var dcv = data.discountCode;
    console.log(dcv);
    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!

1
why not put it in localstorage more about localstorage w3schools.com/jsref/prop_win_localstorage.aspGaurav Gupta
there is more then one way to achieve that you needGaurav Gupta

1 Answers

2
votes

for sending it to checkout page

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

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

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).