How to Add a Sale Countdown Timer to WooCommerce Without a Plugin – 2026

Last updated on December 5th, 2024 at 08:23 am

Adding a countdown timer to WooCommerce sale products is a powerful way to encourage customers to make a purchase before time runs out. While plugins can achieve this, adding a countdown timer without a plugin offers you flexibility, reduces overhead, and allows for customisation.

In this guide, we’ll show you how to add a sale countdown timer directly on your WooCommerce product pages.


Step 1: Add Countdown Timer HTML to Product Pages

To start, let’s add a countdown timer that displays when a product is on sale. This involves adding a small block of HTML on the product page that will display the time remaining in the sale.

Code to Display Countdown Timer HTML

Add the following code to your theme’s functions.php file or a child theme:

/*
 * Snippet: How to Add a Sale Countdown Timer to WooCommerce Without a Plugin – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1645
* Tested with WooCommerce 10.7.0
* "This function adds a countdown timer element to the product page"
*/ function wcsuccess_add_countdown_timer_html() { global $product; if ( $product->is_on_sale() && $product->get_date_on_sale_to() ) { $sale_end_date = $product->get_date_on_sale_to()->getTimestamp() * 1000; // Convert to milliseconds for JavaScript echo '<div id="countdown-timer" data-sale-end="' . esc_attr($sale_end_date) . '" style="font-size: 18px; margin-top: 10px;">'; echo '<strong>Sale ends in: </strong><span id="time-remaining"></span>'; echo '</div>'; } } add_action( 'woocommerce_single_product_summary', 'wcsuccess_add_countdown_timer_html', 25 );

Explanation:

  • Conditional Display: This code only displays the timer if the product is on sale and has a defined end date.
  • Data Attribute: The data-sale-end attribute holds the sale end time in milliseconds, which we’ll use in JavaScript to calculate the remaining time.

Step 2: Add JavaScript to Display the Countdown Timer

Next, we’ll add JavaScript to handle the countdown calculation and dynamically update the timer on the product page.

JavaScript for Countdown Functionality

Add the following JavaScript code to functions.php or enqueue it as a separate file for better organisation:

/*
 * Snippet: How to Add a Sale Countdown Timer to WooCommerce Without a Plugin – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1645
* Tested with WooCommerce 10.7.0
* "This JavaScript creates a countdown timer that updates in real time"
*/ function wcsuccess_enqueue_countdown_script() { if ( is_product() ) { ?> <script type="text/javascript"> document.addEventListener('DOMContentLoaded', function() { var countdownElement = document.getElementById('countdown-timer'); if (countdownElement) { var saleEndTime = parseInt(countdownElement.getAttribute('data-sale-end'), 10); function updateCountdown() { var now = new Date().getTime(); var timeRemaining = saleEndTime - now; if (timeRemaining > 0) { var days = Math.floor(timeRemaining / (1000 * 60 * 60 * 24)); var hours = Math.floor((timeRemaining % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); var minutes = Math.floor((timeRemaining % (1000 * 60 * 60)) / (1000 * 60)); var seconds = Math.floor((timeRemaining % (1000 * 60)) / 1000); countdownElement.querySelector('#time-remaining').textContent = days + "d " + hours + "h " + minutes + "m " + seconds + "s "; } else { countdownElement.querySelector('#time-remaining').textContent = "Sale Ended"; } } // Update countdown every second updateCountdown(); setInterval(updateCountdown, 1000); } }); </script> <?php } } add_action('wp_footer', 'wcsuccess_enqueue_countdown_script');

Explanation:

  • JavaScript Timer: The updateCountdown function calculates the time remaining until the sale end time and updates the display every second.
  • Time Format: The timer displays in the format “Xd Xh Xm Xs” (days, hours, minutes, seconds).
  • Sale End Check: Once the sale ends, the timer shows “Sale Ended”.
See also  How to Calculate Sales by Coupon Code in WooCommerce - 2026

Step 3: Add CSS to Style the Countdown Timer

For a visually appealing timer, add CSS to style the timer display.

CSS Code

Add this CSS to your theme’s style.css file or custom CSS section:

#countdown-timer {
    background-color: #ffeb3b;
    padding: 10px;
    border-radius: 5px;
    color: #333;
    font-weight: bold;
    display: inline-block;
    margin-top: 10px;
}

Explanation:

  • Styling: The timer has a yellow background with padding and rounded corners to make it visually appealing.
  • Customisation: You can adjust colours, padding, and fonts to fit your store’s theme.

Example Workflow

  1. Product Sale Setup: Set a sale price and sale end date for your product in the WooCommerce product editor.
  2. Countdown Display: When the product page loads, the countdown timer displays, showing the time left until the sale ends.
  3. Dynamic Update: The timer updates in real time, creating urgency for customers to complete their purchase before the sale ends.
See also  How to Hide a WooCommerce Category from Search Results - 2026

Conclusion

Adding a countdown timer to WooCommerce products without a plugin provides a powerful way to create urgency and boost conversions. By following this guide, you can display a real-time sale countdown timer directly on your product pages, customised to fit your store’s branding.

Test these changes in a staging environment before applying them to your live store. For more WooCommerce customisations, check out our WooCommerce Visual Hooks Guide or use our wp-config generator for advanced configurations.

0 0 votes
Article Rating

Stay In Touch

Was this post helpful? Why not show your support and buy me a coffee?

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Scroll to Top
0
Would love your thoughts, please comment.x
()
x
×