-
-
Save codex5/9d953bbd2c55df54c79fbece14ce3246 to your computer and use it in GitHub Desktop.
Revisions
-
kartick14 revised this gist
Apr 19, 2018 . 2 changed files with 75 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -34,4 +34,8 @@ $(document).ready(function() { } } }); </script> This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,70 @@ 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> -
kartick14 created this gist
Apr 19, 2018 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ <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>