首页 > 解决方案 > Woo 自定义徽章/气泡条件

问题描述

我创建了一个自定义徽章/气泡“新!” 基于发布时间:

add_action( 'woocommerce_before_shop_loop_item', function() {
#add_action( 'woocommerce_single_product_image_html', function() {

        $postdate      = get_the_time( 'Y-m-d' ); // Post date
        $postdatestamp = strtotime( $postdate );  // Timestamped post date
        $newness       = 10;                      // Newness in days
        if ( ( time() - ( 60 * 60 * 24 * $newness ) ) < $postdatestamp ) {

               echo '<div class="woo-entry-new-badge"><div class="woo-cust-new-badge">' . esc_html__( 'New!', 'woocommerce' ) . '</div></div>';

        }
}, 20 );

但我想添加条件以显示是否未设置“特价”。应该怎么看?

谢谢!

标签: woocommerce

解决方案


WC_Product::is_on_sale() – 返回产品是否在销售。

function my_callback_function() {
    global $product;

    $postdate = get_the_date( 'Y-m-d', $product->get_id() ); // Post date   
    $postdatestamp = strtotime( $postdate ); // Timestamped post date
    $newness = 10; // Newness in days

    $onsale = $product->is_on_sale();

    if ( ( time() - ( 60 * 60 * 24 * $newness ) ) < $postdatestamp && !$onsale ) {
        echo '<div class="woo-entry-new-badge"><div class="woo-cust-new-badge">' . esc_html__( 'New!', 'woocommerce' ) . '</div></div>';
    }
}
add_action( 'woocommerce_before_shop_loop_item', 'my_callback_function', 10, 0 );

推荐阅读