WooCommerce Show Sale Ending Countdown Timer On Loop

WooCommerce product loop showing countdown timer to sale end date


There are no pre-requisites in order for you to be able to implement this solution.

How To Implement This Solution?

Simply copy the pre-coded solution to your active theme’s functions.php or preferably the Code Snippets Plugin.

About this Solution

This snippet increases the Fear Of Missing Out (FOMO) and urgency factor by showing a countdown timer on the product loop.

Specifically, the timer counts down to the sale end price. If there is no sale end price, the countdown timer will not show.

The “sale to price” is set in the product settings under the “General” tab and saved in the database in the “wp_postmeta” table with the meta key “_sale_price_dates_to”.

By showing a countdown, the customer knows that the sale will end, an encourages them to purchase now rather than wait and potentially miss out on the sales price.

 * Snippet Name:	WooCommerce Show Sale Ending Countdown Timer On Loop
 * Snippet Author:	ecommercehints.com with props to www.w3schools.com for the JS countdown

add_action( 'woocommerce_after_shop_loop_item_title', 'ecommercehints_show_countdown_timer_on_loop', 15 );
function ecommercehints_show_countdown_timer_on_loop() {
	global $product;
    $sales_price_to   = date( "M j, Y", get_post_meta( $product->id, '_sale_price_dates_to', true )); // Get the sales price 'to' date
	if ( $product->is_on_sale() && $sales_price_to != "" ) {	
		echo 'Sales ends in:<br>
		<p id="timer"></p>
			// Set the date we are counting down to (sale price to date)
			var countDownDate = new Date("'.$sales_price_to.'").getTime();
			// Update the count down every 1 second
			var x = setInterval(function() {
			// Get date and time for today
			var now = new Date().getTime();
			// Find the distance between now and the count down date
			var distance = countDownDate - now;
			// Time calculations for days, hours, minutes and seconds
			var days = Math.floor(distance / (1000 * 60 * 60 * 24));
			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);
			// Output the result in an element with id="timer"
			document.getElementById("timer").innerHTML = days + " days,  " + hours + " hours,  " + minutes + " minutes,  " + seconds + " seconds ";
			// If the count down is over, write some text
			if (distance < 0) {
				document.getElementById("timer").innerHTML = "SALE ENDED";
			}}, 1000);

Snippet Benefits

  • Increase Fear Of Missing Out (FOMO).
  • Increase Urgency.
  • Show a product specific countdown timer on the product loop.
WooCommerce Conversion Rate Optimisation (CRO) eBook
100 WooCommerce Conversion Rate Optimisation Tips

2 Responses

  1. 1. Doesn’t show anything after “Sales ends in”. I thought it might be a theme issue for me, so I tried on Storefront theme. Same result. Doesn’t show the actual timer.

    2. Would be really great to get code snippet to show the same timer on single product page too.

    1. Hey Manish,
      I’ve just tried again my end and it seems ok. Although I did just update the JS part of the snippet. Could you try again please and let me know? Keep in mind this snippet is for the product archive template not the single product template – it will be a case of using the same code but a different hook…this is a steer in the right direction (WooCommerce Product Page Countdown Timer). If you’d like me to implement on your site, drop me a message 😀 https://ecommercehints.com/hire-me

Leave a Reply

If you are going to write code in the comments, please wrap it between code tags.

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

Other Recent Guides

Subscribe To Emails

Get exclusive WooCommerce tips that I only share with email subscribers

Join hundreds of other subscribers