Last active
July 27, 2023 13:19
-
-
Save kartick14/29ad023340267e9b063c70b4e191160d to your computer and use it in GitHub Desktop.
Apply discount code from cart page in shopify
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="discountcodefield"> | |
<label for="discount">Discount Code:</label> | |
<input autocomplete="off" type="hidden" name="discount" class="discount_code" /> | |
<input autocomplete="off" type="text" name="discount_code" class="discount_code_field" /> | |
<input type="button" name="apply_discount_code" class="btn discount_code_btn" value="Apply"/> | |
</div> | |
<div class="cart__savings discount_apply_code"> | |
</div> | |
<script type="text/javascript"> | |
$(document).ready(function() { | |
$('.discount_code_btn').click(function(){ | |
var discountStored = $('input[name="discount_code"]').val(); | |
$('input[name="discount"]').val(discountStored); | |
$('.discount_apply_code').html('<div>Discount code <strong>'+discountStored+'</strong> applied on checkout page. <a href="javascript:void(0);" class="clear-discount">X</a></div>'); | |
localStorage.setItem('storedDiscount', discountStored); | |
$('input[name="discount_code"]').val(''); | |
}); | |
$(document).on("click", 'a.clear-discount', function(event) { | |
var discountStored = ''; | |
$('input[name="discount"]').val(discountStored); | |
$('.discount_apply_code').html(''); | |
var deletenum = 'storedDiscount'; | |
localStorage.removeItem(deletenum); | |
}); | |
if (localStorage.getItem('storedDiscount')){ | |
var discountStored = localStorage.getItem('storedDiscount'); | |
if(discountStored != '' && discountStored != 'undefined'){ | |
$('input[name="discount"]').val(localStorage.getItem('storedDiscount')); | |
$('.discount_apply_code').html('<div>Discount <strong>'+discountStored+'</strong> applied on checkout page. <a href="javascript:void(0);" class="clear-discount">X</a></div>'); | |
} | |
} | |
}); | |
</script> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Put the below code into checkout page | |
<script> | |
(function () { | |
var jquery = null; | |
if (window.jQuery) { | |
jquery = window.jQuery; | |
} else if (window.Checkout && window.Checkout.$) { | |
jquery = window.Checkout.$; | |
} | |
jquery(document).on("click", 'button.applied-reduction-code__clear-button', function(event) { | |
var deletenum = 'storedDiscount'; | |
localStorage.removeItem(deletenum); | |
var url = window.location.href; console.log(url); | |
var return_url = removeURLParameter(url, 'discount'); | |
window.location.href = return_url; | |
}); | |
jquery(document).on("click", 'button.field__input-btn', function(event) { | |
var value = jquery('#checkout_reduction_code').val(); | |
updateQueryStringParam('discount', value); | |
}); | |
})(); | |
function removeURLParameter(url, parameter) { | |
//prefer to use l.search if you have a location/link object | |
var urlparts= url.split('?'); | |
if (urlparts.length>=2) { | |
var prefix= encodeURIComponent(parameter)+'='; | |
var pars= urlparts[1].split(/[&;]/g); | |
//reverse iteration as may be destructive | |
for (var i= pars.length; i-- > 0;) { | |
//idiom for string.startsWith | |
if (pars[i].lastIndexOf(prefix, 0) !== -1) { | |
pars.splice(i, 1); | |
} | |
} | |
url= urlparts[0]+'?'+pars.join('&'); | |
return url; | |
} else { | |
return url; | |
} | |
} | |
// Explicitly save/update a url parameter using HTML5's replaceState(). | |
function updateQueryStringParam(key, value) { | |
baseUrl = [location.protocol, '//', location.host, location.pathname].join(''); | |
urlQueryString = document.location.search; | |
var newParam = key + '=' + value, | |
params = '?' + newParam; | |
// If the "search" string exists, then build params from it | |
if (urlQueryString) { | |
keyRegex = new RegExp('([\?&])' + key + '[^&]*'); | |
// If param exists already, update it | |
if (urlQueryString.match(keyRegex) !== null) { | |
params = urlQueryString.replace(keyRegex, "$1" + newParam); | |
} else { // Otherwise, add it to end of query string | |
params = urlQueryString + '&' + newParam; | |
} | |
} | |
window.history.replaceState({}, "", baseUrl + params); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Quick question - this solution will only work for Shopify Plus customers, as other than that "checkout.liquid" file is not visible, right ?