I tried to add the label “new” to products created within the last 30 days by adding an action to the “woocommerce_before_shop_loop_item” hook. However, it’s not working.
function show_new_badge() {
global $product;
$newness_days = 30; // Set days as new
$created = strtotime( $product->get_date_created() );
if ( ( time() - ( 60 * 60 * 24 * $newness_days ) ) < $created ) {
echo '<span class="wc-new-badge">' . esc_html__( 'New', 'woocommerce' ) . '</span>';
}
}
add_action( 'woocommerce_before_shop_loop_item', 'show_new_badge', 1 );
I tried debugging by placing “die()” after “echo” and found that this function works; “New” text is printed to the screen. However, when “die()” is removed, the “New” text disappears.