javascript - WooCommerce 产品倒计时
问题描述
我创建函数来获取日期并将其转为倒计时。这是我的功能代码:
function countdown() {
global $product;
$start = get_post_meta( get_the_ID() , 'yung_product_date_start', true );
$end = get_post_meta( get_the_ID() , 'yung_product_date_end', true );
if ( $start && $end ) {
$current = strtotime(date("Y/m/d"));
$date = strtotime($start);
$end_date = strtotime($end);
if (( $date == $current || $current > $date ) && $current != $end_date && $end_date > $current) {
wp_enqueue_script( "countdown", get_template_directory_uri() . '/assets/js/countdown.js', "1.0", true );
wp_localize_script(
"countdown",
'countdown_js',
array(
'to' => $end,
'from' => $start
)
);
echo '<div id="product-countdown">
<span id="countdown-counter"></span>
</div>';
}
}
}
add_action( 'woocommerce_before_add_to_cart_form', 'countdown' );
这是我在“countdown.js”文件中的javascript代码:
jQuery(function($){
"use strict";
var from = countdown_js.from;
var to = countdown_js.to;
// Update the count down every 1 second
var countDownDate = new Date(to).getTime();
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);
if ( days<10 ) {
var days = '0' + days;
}
if ( hours<10 ) {
var hours = '0' + hours;
}
if ( minutes<10 ) {
var minutes = '0' + minutes;
}
if ( seconds<10 ) {
var seconds = '0' + seconds;
}
if ( days == 0 ) {
var days = '';
} else {
days += ' : '
}
$('#countdown-counter').each( function() {
var html = days + hours + " : " + minutes + " : " + seconds;
$(this).html(html);
});
if (distance < 0) {
clearInterval(x);
$('#product-countdown').css("display", "none");
}
}, 1000);
});
此代码适用于单个产品页面,但是当我在产品存档中尝试此代码时,它仅适用于第一个产品,不适用于其余产品。我希望每个产品都有自己的倒计时。我该怎么办请帮帮我!
解决方案
感谢 pianka 帮助我解决了这个问题。我终于编辑了这些代码,它终于奏效了。
function countdown() {
global $product;
$start = get_post_meta( get_the_ID() , 'yung_product_date_start', true );
$end = get_post_meta( get_the_ID() , 'yung_product_date_end', true );
if ( $start && $end ) {
$current = strtotime(date("Y/m/d"));
$date = strtotime($start);
$end_date = strtotime($end);
if (( $date == $current || $current > $date ) && $current != $end_date && $end_date > $current) {
wp_enqueue_script( "countdown", get_template_directory_uri() . '/assets/js/countdown.js', "1.0", true );
echo '<div class="product-countdown">
<span class="countdown-counter" countdown="'. $end .'"></span>
</div>';
} ?>
<?php
}
}
add_action( 'woocommerce_before_add_to_cart_form', 'countdown' );
和javascript代码:
jQuery(function($){
"use strict";
$('.countdown-counter').each( function() {
var to = $(this).attr("countdown");
var thisis = $(this);
var parent = $(this).parent();
var countDownDate = new Date(to).getTime();
// 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);
if ( days<10 ) {
var days = '0' + days;
}
if ( hours<10 ) {
var hours = '0' + hours;
}
if ( minutes<10 ) {
var minutes = '0' + minutes;
}
if ( seconds<10 ) {
var seconds = '0' + seconds;
}
if ( days == 0 ) {
var days = '';
} else {
days += ' : '
}
var html = days + hours + " : " + minutes + " : " + seconds;
thisis.html(html);
if (distance < 0) {
clearInterval(x);
parent.css("display", "none");
}
}, 1000);
thisis.removeAttr("countdown");
});
});
推荐阅读
- networking - 关于在 WPA2 网络中拦截数据包的问题
- python - 关于 Turtle 中的循环
- python - Python3 - 如何在字典列表中按值搜索键?
- csound - 一个项目中有 2 个 Midi 文件
- javascript - 在html中选择某些选项时如何使某些输入字段出现?
- html - 首次在浏览器中显示时,如何让雪层和底层图像表现得完全响应?
- sql - 查找 SQL 记录是否在随后的时间段内不再存在的最佳方法是什么
- c++ - 在 C++ 中定义 unique_ptr 时的 T 类与 T 类?
- r - 如何在 r 中列出远程 unix 服务器上的文件名?
- swift - 将 onChanged 回调添加到 SwiftUI 结构以发出值