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-endattribute 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
updateCountdownfunction 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”.
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
- Product Sale Setup: Set a sale price and sale end date for your product in the WooCommerce product editor.
- Countdown Display: When the product page loads, the countdown timer displays, showing the time left until the sale ends.
- Dynamic Update: The timer updates in real time, creating urgency for customers to complete their purchase before the sale ends.
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.
I have been working with WordPress and WooCommerce since 2012 and have developed a deep knowledge of the content management system. Since 2012, I have developed several plugins and designed dozens of websites utilising different frameworks, CMS’s and programming languages. I am proficient in PHP, Python, Java, C, C++, R and JavaScript with limited experience in Go, Kotlin and Swift.
Educationally, I have a Master’s degree in cyber security a Bachelor’s (Hons, First Class) in Applied Research and a Graduate Certificate in Data Science. I’m currently undertaking PhD studies investigating IoT cybersecurity. I recently graduated with First Class Honours and Masters of Information Technology, receiving the Executive Dean’s Award for studies undertaken in the 2021 and 2022 academic years. I have worked in the information technology industry for the past 11 years primarily as a software/web developer specific to design, optimisation, network management and security. My research interests are in the areas of Internet of Things (IoT), 5G and Beyond Networks, information security for wireless networks and software development.
Stay In Touch

