“New” badge on recent products
Products added in the last 30 days show a “New” label in the shop and category pages.
<?php
add_action(
'woocommerce_before_shop_loop_item_title',
static function () {
global $product;
$days = 30;
if ( $product instanceof WC_Product && $product->get_date_created() && $product->get_date_created()->getTimestamp() > time() - $days * DAY_IN_SECONDS ) {
echo '<span class="snipfire-new">' . esc_html__( 'New', 'woocommerce' ) . '</span>';
}
},
9
);
add_action(
'wp_head',
static function () {
echo '<style>.woocommerce ul.products li.product{position:relative}.snipfire-new{position:absolute;top:8px;right:8px;z-index:2;background:#2346c9;color:#fff;font-size:.75rem;font-weight:600;text-transform:uppercase;letter-spacing:.04em;padding:4px 8px;border-radius:3px}</style>';
}
);
Good to know
Change $days to suit your shop. For classic shop templates.
In these packs
- WooCommerce: shop and product pages: Badges, price display, tabs and layout for your shop and product pages.