Woocommerce-检查产品是否在60天之前创建 [英] Woocommerce - Check if product was created less than 60 days ago

查看:29
本文介绍了Woocommerce-检查产品是否在60天之前创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查Woocommerce产品是否在不到60天之前创建.-如果为true,请执行某些操作.

I want to check if a Woocommerce product was created less than 60 days ago. - If true, do something.

我正在使用官方的Woocmmerce函数 $ product-> get_date_created 在后端/管理员中获取创建产品的日期.

I am obtaining the date the product was created in backend/admin using the official Woocmmerce function $product->get_date_created.

我的代码部分起作用,但是它似乎正在检查 $ product-> get_date_created 从字面上看是否包含值60 ,而不是执行计算并减少60天当前DateTime .

My code partially works, but it seems to be checking if $product->get_date_created literally contains the value 60 instead of perfoming the calculation and minusing 60 days from the current DateTime.

我之所以得出这个结论,是因为我的IF语句运行正确,并且应用于实际DateTime字符串中带有"60"的所有产品.(例如12/31/2060)...这不是我想要的.

I have come to this conclusion because my IF statement runs true and is applied to all products with "60" in the actual DateTime string. (e.g. 12/31/2060)...this is not what I want.

任何帮助表示赞赏.

我的代码:

add_action( 'woocommerce_before_shop_loop_item_title', 'display_new_loop_woocommerce' );

function display_new_loop_woocommerce() {
    global $product;

  // Get the date for the product published and current date
  $start = date( 'n/j/Y', strtotime( $product->get_date_created() ));
  $today = date( 'n/j/Y' );

  // Get the date for the start of the event and today's date.
  $start      = new \DateTime( $start );
  $end        = new \DateTime( $today );

    // Now find the difference in days.
  $difference = $start->diff( $end );
  $days      = $difference->d;

    // If the difference is less than 60, apply "NEW" label to product archive.             
    if ( $days = (60 < $days) ) {
        echo '<span class="limited">' . __( 'NEW', 'woocommerce' ) . '</span>';
    }
} 

推荐答案

我已经改用 WC_DateTime 方法重新访问了您的代码,该方法将保留商店中的时区设置:

I have revisited a bit your code using the WC_DateTime methods instead, which will keep the time zone settings from the shop:

add_action( 'woocommerce_before_shop_loop_item_title', 'display_new_loop_woocommerce' );
function display_new_loop_woocommerce() {
    global $product;

    // Get the date for the product published and current date
    $datetime_created  = $product->get_date_created(); // Get product created datetime
    $timestamp_created = $datetime_created->getTimestamp(); // product created timestamp

    $datetime_now      = new WC_DateTime(); // Get now datetime (from Woocommerce datetime object)
    $timestamp_now     = $datetime_now->getTimestamp(); // Get now timestamp

    $time_delta        = $timestamp_now - $timestamp_created; // Difference in seconds
    $sixty_days        = 60 * 24 * 60 * 60; // 60 days in seconds

    // If the difference is less than 60, apply "NEW" label to product archive.
    if ( $time_delta < $sixty_days ) {
        echo '<span class="limited">' . __( 'NEW', 'woocommerce' ) . '</span>';
    }
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于Woocommerce-检查产品是否在60天之前创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆