Hello, I am looking for help calling two scripts on my woocommerce product and cart page. I would like to be able to insert the script and code in functions.php and not load script from file.
On product page load this script
<script>
var countDownDate = new Date().getTime()+(4*60*60*1000+20*60*1000+7*1000);
// Update the count down every 1 second
var x = setInterval(function() {
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now an the count down date
var distance = countDownDate - now;
// +20*60*1000+7*1000Time calculations for days, hours, minutes and seconds
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the result in the element with id="demo"
document.getElementById("demo").innerHTML = hours + "hrs "+ minutes + "mins " + seconds + "secs";
// If the count down is finished, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
</script>
On cart page and checkout page load this script
<script>
window.onload = function() {
var display = document.querySelector('#time'),
timer = new CountDownTimer(300);
timer.onTick(format).onTick(restart).start();
function restart() {
if (this.expired()) {
setTimeout(function() { timer.start(); }, 1000);
}
}
function format(minutes, seconds) {
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.textContent = minutes + ':' + seconds;
}
};
</script>
thank you
Reigel Gallarde answers:
hello this should do it...
function my_wc_custom_script() {
// on checkout or cart page
if ( is_checkout() || is_cart() ) {
?>
<script>
window.onload = function() {
var display = document.querySelector('#time'),
timer = new CountDownTimer(300);
timer.onTick(format).onTick(restart).start();
function restart() {
if (this.expired()) {
setTimeout(function() { timer.start(); }, 1000);
}
}
function format(minutes, seconds) {
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.textContent = minutes + ':' + seconds;
}
};
</script>
<?php
}
// on product page
if ( is_product() ){
?>
<script>
var countDownDate = new Date().getTime()+(4*60*60*1000+20*60*1000+7*1000);
// Update the count down every 1 second
var x = setInterval(function() {
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now an the count down date
var distance = countDownDate - now;
// +20*60*1000+7*1000Time calculations for days, hours, minutes and seconds
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the result in the element with id="demo"
document.getElementById("demo").innerHTML = hours + "hrs "+ minutes + "mins " + seconds + "secs";
// If the count down is finished, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
</script>
<?php
}
}
add_action( 'wp_footer', 'my_wc_custom_script', 9999 );
the above code without the script is this:
function my_wc_custom_script() {
// on checkout or cart page
if ( is_checkout() || is_cart() ) {
?>
// your javascript code goes here..
<?php
}
// on product page
if ( is_product() ){
?>
// your javascript code goes here..
<?php
}
}
add_action( 'wp_footer', 'my_wc_custom_script', 9999 );
Reigel Gallarde comments:
Please test this code and let me know if there's more that I can help.
Cheers!
Reigel
http://reigelgallarde.me
Reigel Gallarde comments:
Hello,
Got any problem with this solution?
Let me know.
Cheers!
Reigel
http://reigelgallarde.me