在 WooCommerce 单个产品页面上显示产品的年龄

Show how old a product is on WooCommerce single product page

通过使用操作挂钩 woocommerce_before_add_to_cart_form,我试图通过检查发布日期是否早于三个月来检查产品的年龄。

如果且仅当产品是在三个月前或更长时间前发布的,在产品页面上使用 DIV 标签显示消息。

此处的总体思路是鼓励访问者和注册客户在产品售罄之前购买该产品,其中可用性意味着产品目录中不再有货 - 就像永久删除产品一样。

这是我正在使用的代码:

add_action( 'woocommerce_before_add_to_cart_form', 'encourage_product_purchase' );
function encourage_product_purchase() {
    
    $product_published = 'Availabe since ' . human_time_diff(get_the_time('U'),current_time('timestamp'));
    
    $three_months = '3';

        if ( $product_published >= get_the_time( $three_months ) ) {

            ?>
            <div class="three-month-product">This product has been available for three months and will soon be removed from our catalog. You have 10 days left to purchase.</div>
            <?php

        }
}

您可以使用$product->get_date_created();

所以你得到:

function action_woocommerce_before_add_to_cart_form() {
    // Get the global product object
    global $product;

    // Is a WC product
    if ( is_a( $product, 'WC_Product' ) ) {
        // Get product date created
        $product_date_created  = $product->get_date_created()->date( 'Y-m-d' );

        // Get now datetime (from Woocommerce datetime object)
        $datetime_now = new WC_DateTime();
        
        // Three months ago from today
        $three_months_ago = date( 'Y-m-d', strtotime( '-3 months', strtotime( $datetime_now ) ) );
        
        // Compare
        if ( strtotime( $product_date_created ) < strtotime( $three_months_ago ) ) {
            echo '<div class="three-month-product">' . __('This product has been available for three months and will soon be removed from our catalog. You have 10 days left to purchase.', 'woocommerce' ) . '</div>';
        }
    }
}
add_action( 'woocommerce_before_add_to_cart_form', 'action_woocommerce_before_add_to_cart_form', 10, 0 );

以下函数将添加自定义消息 仅基于产品创建日期:

  • 如果产品创建日期大于或等于 3 个月
  • 如果您在 产品页面

代码已经过测试并且有效。

// displays a customized message on the product page based on the product creation date
add_action( 'woocommerce_before_add_to_cart_form', 'encourage_product_purchase', 10 );
function encourage_product_purchase() {

    // only on the single product page
    if ( ! is_product() ) {
        return;
    }

    // gets the product object
    global $product;
    if ( ! is_object( $product ) ) {
        $product = wc_get_product( get_the_ID() );
    }

    // get the product creation date in "Y-m-d" format
    $product_date_created  = $product->get_date_created()->format('Y-m-d');
    // gets the date three months ago from today
    $date_three_months_ago = date( "Y-m-d", strtotime( '-3 months' ) );

    // if the product was created 3 months ago or more it displays a message
    if ( $product_date_created <= $date_three_months_ago ) {
        echo '<div class="three-month-product">' . __( 'This product has been available for three months and will soon be removed from our catalog. You have 10 days left to purchase.', 'woocommerce' ) . '</div>';
    }

}

将其添加到您的活动主题的 functions.php 中。