Home > Mobile >  Countdown for Woocommerce product on Archive / Loop page
Countdown for Woocommerce product on Archive / Loop page

Time:11-05

I've made a countdown to display the sale date end using the meta key _sale_price_to . See below the code :

add_shortcode( 'woocommerce_timer_two', 'sales_timer_countdown_product_two', 20 );
function sales_timer_countdown_product_two($atts) {
            extract( shortcode_atts( array(
        'id' => get_the_ID(),
    ), $atts, 'woocommerce_timer_two' ) ); 

    global $product;
    
         // If the product object is not defined, we get it from the product ID
    if ( ! is_a($product, 'WC_Product') && get_post_type($id) === 'product' ) {
        $product = wc_get_product($id);
    }
    
    if ( is_a($product, 'WC_Product') ) {

    $sale_date = get_post_meta( $product->get_id(), '_sale_price_dates_to', true );
    
    if ( ! empty( $sale_date ) ) {
        
        ?>
        <script>
            
        jQuery(function($){
        "use strict";

        $('.countdown-counter').each( function() {
        var to = $(this).attr("countdown");
        var thisis = $(this);
        var parent = $(this).parent();
        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="sale-end"
                var html = days   hours   " : "   minutes   " : "   seconds;
                thisis.html(html);
                    
                // If the count down is over, write some text 
                if (distance < 0) {
                clearInterval(x);
                parent.css("display", "none");
            }
        }, 1000);
        thisis.removeAttr("countdown");
        });
        });
        </script>
        <!-- this is where the countdown is displayed -->
        <div >
             <span  countdown="'. $html .'"></span>
        </div>;
        <?php
    }
    }
}

The code works on the product single page but I need it on the archive and loop page. On the archive page, all the products have the same value for the countdown. I guess it's because I can't give an attribute for each archive item.

Related posts that may help :

CodePudding user response:

if this code is working on product single page then you can add an action hook in archive/loop page using WooCommerce hooks.

example :-

add_action( 'woocommerce_after_shop_loop_item_title', 'zillion_countdown_show_in_loop', 20 );
function zillion_countdown_show_in_loop()
{
    do_shortcode('[woocommerce_timer_two]');
 
}

EDITED

you can replace the class with product id added with it.

$('.countdown-counter<?php echo $product->get_id();?>')

replace html part also.

<div >
    <span  countdown="'. $html .'"></span>
</div>
  • Related