Search code examples
javascriptphpwordpresswoocommercecountdown

Countdown timer until product sale ends on WooCommerce single product page


My goal with this, as I am learning, is to add a countdown timer after the add to cart form on the product page that counts down the time left until the sale is over.

I'm using "How TO - JavaScript Countdown Timer" from the w3schools website and I wrote code for getting the _sale_price_dates_to using $sale_date = get_post_meta( $product->get_id(), '_sale_price_dates_to', true );

My problem is this: nothing shows on the product page. No notice, no errors and nothing in the error log. I believe that this is the problem, but I'm not sure: var countDownDate = <?php $sale_date; ?>

The code so far:

add_action( 'woocommerce_after_add_to_cart_form', 'sales_timer_countdown_product', 20 );
function sales_timer_countdown_product() {  

    global $product;

    $sale_date = get_post_meta( $product->get_id(), '_sale_price_dates_to', true );
        if (!empty( $sale_date ) ) { ?>

    <script>
        // Set the date we're counting down to
        var countDownDate = <?php $sale_date; ?>

        // Update the count down every 1 second
        var x = setInterval(function() {

        // Get today's date and time
        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="saleend"
        document.getElementById("saleend").innerHTML = days + "d " + hours + "h "
        + minutes + "m " + seconds + "s ";
            
        // If the count down is over, write some text 
        if (distance < 0) {
            clearInterval(x);
            document.getElementById("saleend").innerHTML = "The sale for this product has EXPIRED";
        }
        }, 1000);
    </script>

    <!-- this is where the countdown is displayed -->
    <p id="saleend" style="color:red"></p>
    <?php
    }
}

Solution

  • Multiply by 1000 because Date() requires miliseconds

    function sales_timer_countdown_product() {  
    
        global $product;
    
        $sale_date = get_post_meta( $product->get_id(), '_sale_price_dates_to', true );
        
        if ( ! empty( $sale_date ) ) {
            ?>
            <script>
                // Set the date we're counting down to
                var countDownDate = <?php echo $sale_date; ?> * 1000;
    
                // Update the count down every 1 second
                var x = setInterval(function() {
                    // Get today's date and time
                    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="saleend"
                    document.getElementById("saleend").innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
                        
                    // If the count down is over, write some text 
                    if (distance < 0) {
                        clearInterval(x);
                        document.getElementById("saleend").innerHTML = "The sale for this product has EXPIRED";
                    }
                }, 1000);
            </script>
    
            <!-- this is where the countdown is displayed -->
            <p id="saleend" style="color:red"></p>
            <?php
        }
    }
    add_action( 'woocommerce_after_add_to_cart_form', 'sales_timer_countdown_product', 20 );