Created
July 8, 2021 17:33
-
-
Save jorpdesigns/4adb232a1508f25496a99b3e8a3dc0d6 to your computer and use it in GitHub Desktop.
Snippet to display JS countdown timer on WooCommerce product page
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 characters
| <?php | |
| // Add this function to any hook listed here - https://www.businessbloomer.com/woocommerce-visual-hook-guide-single-product-page/ | |
| function delivery_countdown() { | |
| date_default_timezone_set('Europe/London'); // Specify preferred time zone | |
| $deadline = 12; // 0 = 12AM, 12 = 12PM, 23 = 11PM | |
| $isWeekend = null; | |
| $weekend = array('Saturday', 'Sunday'); | |
| $weekdayDeadline = "Friday 12PM"; | |
| if ( in_array( date("l"), $weekend ) || ( strtotime("now") > strtotime($weekdayDeadline) ) ) { | |
| $isWeekend = true; | |
| } else { | |
| $isWeekend = false; | |
| } | |
| if ( ! $isWeekend ) { | |
| echo ' | |
| <p><span id="delivery-countdown"></span></p> | |
| <script> | |
| jQuery( function( $ ) { | |
| $(document).ready(function(){ | |
| function countdownTimer() { | |
| let currentTime = new Date(); | |
| let deadline = ' . $deadline . '; | |
| deadline -= 1; | |
| let hrs, mins, secs, hrsPrefix, minsPrefix, secsPrefix; | |
| if (currentTime.getHours() > deadline) { | |
| hrs = (deadline - currentTime.getHours()) + 24; | |
| if (currentTime.getDay() == 5) { | |
| hrs += 48; | |
| } | |
| if (currentTime.getDay() == 6) { | |
| hrs += 24; | |
| } | |
| } else { | |
| hrs = deadline - currentTime.getHours(); | |
| } | |
| mins = 59 - currentTime.getMinutes(); | |
| secs = 59 - currentTime.getSeconds(); | |
| if (hrs == 1) { | |
| hrsPrefix = " hour "; | |
| } else { | |
| hrsPrefix = " hours "; | |
| } | |
| if (mins == 1) { | |
| minsPrefix = " minute "; | |
| } else { | |
| minsPrefix = " minutes "; | |
| } | |
| if (secs == 1) { | |
| secsPrefix = " second"; | |
| } else { | |
| secsPrefix = " seconds"; | |
| } | |
| timeLeft = hrs + hrsPrefix + mins + minsPrefix + secs + secsPrefix; | |
| $("#delivery-countdown").html(timeLeft); | |
| } | |
| setInterval(countdownTimer ,1000); | |
| }); | |
| }); | |
| </script> | |
| '; | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment