"New" Badge for Recent Products
Shows a "New" label on products in the shop that were published in the last 30 days.
Opens WPIDE on
Code
add_action( 'woocommerce_before_shop_loop_item_title', function () {
global $product;
$days = 30; // Products newer than this many days get the badge.
if ( ! $product instanceof WC_Product || ! $product->get_date_created() ) {
return;
}
$age = time() - $product->get_date_created()->getTimestamp();
if ( $age > $days * DAY_IN_SECONDS ) {
return;
}
echo '<span class="wpide-new-badge" style="position:absolute;top:10px;right:10px;z-index:9;padding:4px 10px;border-radius:3px;background:#2271b1;color:#fff;font-size:12px;font-weight:600;text-transform:uppercase;">' . esc_html( 'New' ) . '</span>';
}, 9 );
add_action( 'wp_head', function () {
echo '<style>.woocommerce ul.products li.product{position:relative;}</style>' . "\n";
} );
Notes
Change $days to control how long a product counts as new.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.