Code Check: Adding a timer to checkout on woocommerce

I’m trying to use this code to echo my JS in PHP for adding a timer and badge to my checkout on woocommerce. Can someone help me fix this?

function add_jscript() {
if (window.location.href.indexOf('checkout') > -1) {
function createCookie(a,b,c){
if(c){
var d=new Date;d.setTime(d.getTime()+c*60*1e3);var e="; expires="+d.toUTCString()
}else var e="";document.cookie=a+"="+b+e+"; path=/"
}
function readCookie(a){
for(var b=a+"=",c=document.cookie.split(";"),d=0;d<c.length;d++){
for(var e=c[d];" "==e.charAt(0);)e=e.substring(1,e.length);
if(0==e.indexOf(b))return e.substring(b.length,e.length)
}
return null}
function eraseCookie(a){
createCookie(a,"",-1)
}
function crtmedir(a){
var b=document.createDocumentFragment(),c=document.createElement("div");for(c.innerHTML=a;c.firstChild;)b.appendChild(c.firstChild);
return b}
function startTimer(start, duration, display){
var diff,minutes,seconds;function timer(){
diff=duration-(((Date.now()-start)/1000)|0);
minutes=(diff / 60)|0;
seconds = (diff % 60)|0;
minutes = minutes < 10 ? "0" + minutes : minutes;seconds = seconds < 10 ? "0" + seconds : seconds;display.textContent = minutes + ":" + seconds;
if (diff <= 0){
clearInterval(inti);document.getElementById("countdownhere").innerHTML = "Order reservation ended.";start = Date.now() + 1000;
}
};
timer();
var inti = setInterval(timer, 1000);}var pdm = crtmedir('<div class="countdownholder"><div id="countdownhere" style="display:block;background:#ededed;padding:10px 20px;border:1px solid #e3df74;font-size:14px;color:#2c2c2c;font-weight:bold;-webkit-border-radius: 5px;-moz-border-radius: 5px;border-radius: 5px; margin:20px 0px">Your order is reserved for <span id="time"></span> minutes!</div><div style="display:table;width:100%"><div style="width:33.3%;display:table-cell;float:none;height:100%;vertical-align:middle;text-align:center"><img src="https://cdn.shopify.com/s/files/1/1319/2435/t/3/assets/truste.png" style="margin:0 auto"></div><div style="width:50%;display:table-cell;float:none;height:100%;vertical-align:middle;text-align:center"><img src="https://cdn.shopify.com/s/files/1/1319/2435/t/3/assets/mcafeesecured.png" style="margin:0 auto"></div></div></div>');
    };
}>';
}

add_action( 'woocommerce_checkout_before_customer_details', 'add_jscript');

I’ve also tried adding this code as a js to my server and calling it from there but it hasn’t worked. Looking forward for suggestions.

Read more here: Code Check: Adding a timer to checkout on woocommerce

Leave a Reply

Your email address will not be published. Required fields are marked *