javascript - WooCommerce 单一产品页面上的倒计时器,直到产品销售结束

标签 javascript php wordpress woocommerce countdown

据我所知,我的目标是在产品页面上的“添加到购物车”表单之后添加一个倒计时器,用于倒计时直到销售结束为止的剩余时间。

我正在使用 w3schools 网站上的“How TO - JavaScript Countdown Timer ”,并且我编写了获取 _sale_price_dates_to 的代码使用$sale_date = get_post_meta( $product->get_id(), '_sale_price_dates_to', true );

我的问题是这样的: 产品页面上没有显示任何内容。没有通知,没有错误,错误日志中没有任何内容。我相信这就是问题所在,但我不确定:var countDownDate = <?php $sale_date; ?>

到目前为止的代码:

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
    }
}

最佳答案

乘以 1000,因为 Date() 需要毫秒

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 );

关于javascript - WooCommerce 单一产品页面上的倒计时器,直到产品销售结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62625177/

相关文章:

javascript - 如何使用 Javascript 更改 API 响应

php - 更改 Woocommerce 中的 "You cannot add another (product) to your cart"通知

javascript - 在 Angular 的 window 对象中存储值有什么好处?

php - 更新使用下划线构建的主题

php - 在 PHP 中指向 "line 0"时出错

php - 如何在jquery中获取wordpress的COOKIEPATH?

php - 过多的 DOM 元素货币符号

javascript - jQuery 在下拉选择中将两个值相乘

javascript - 查明最后一个站点是否与原点相同

javascript - jQuery: if (dd).class = active then (dd span).text = 示例文本