存档/循环页面上的 Woocommerce 产品倒计时
P粉237647645
P粉237647645 2023-08-31 21:06:59
0
1
249
<p>我使用元键 <code>_sale_price_to</code> 进行了倒计时以显示销售日期结束。请参阅下面的代码:</p> <pre class="brush:php;toolbar:false;">add_shortcode( 'woocommerce_timer_two', 'sales_timer_countdown_product_two', 20 ); function sales_timer_countdown_product_two($atts) { extract( shortcode_atts( array( 'id' =&gt; 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') &amp;&amp; get_post_type($id) === 'product' ) { $product = wc_get_product($id); } if ( is_a($product, 'WC_Product') ) { $sale_date = get_post_meta( $product-&gt;get_id(), '_sale_price_dates_to', true ); if ( ! empty( $sale_date ) ) { ?&gt; &lt;script&gt; jQuery(function($){ &quot;use strict&quot;; $('.countdown-counter').each( function() { var to = $(this).attr(&quot;countdown&quot;); var thisis = $(this); var parent = $(this).parent(); var countDownDate = &lt;?php echo $sale_date; ?&gt; * 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=&quot;sale-end&quot; var html = days + hours + &quot; : &quot; + minutes + &quot; : &quot; + seconds; thisis.html(html); // If the count down is over, write some text if (distance &lt; 0) { clearInterval(x); parent.css(&quot;display&quot;, &quot;none&quot;); } }, 1000); thisis.removeAttr(&quot;countdown&quot;); }); }); &lt;/script&gt; &lt;!-- this is where the countdown is displayed --&gt; &lt;div class=&quot;product-countdown&quot;&gt; &lt;span class=&quot;countdown-counter&quot; countdown=&quot;'. $html .'&quot;&gt;&lt;/span&gt; &lt;/div&gt;; &lt;?php } } }</pre> <p>该代码适用于产品单页面,但我需要它在存档和循环页面上。在存档页面上,所有产品都具有相同的倒计时值。我想这是因为我无法为每个存档项目提供属性。</p> <p>可能有帮助的相关帖子:</p> <ul> <li>Link 1</li> <li>Link 2</li> </ul></p>
P粉237647645
P粉237647645

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!