Add a “New” badge to recent products
In e-commerce, new products naturally attract curiosity. A “New” badge helps to distinguish them effortlessly, boosting clicks and giving your store a fresh feel. The problem is that WooCommerce does not offer this basic feature. Fortunately, a small piece of well-placed code solves the problem without the need for unnecessary plugins.
This snippet checks the publication date of each product displayed in the WooCommerce store. It compares this date to the current date to determine whether the product is recent enough—in this case, less than 30 days old—to be considered “new.” If so, it inserts a small HTML block containing the “New” badge just before the product title in the display loop. The badge is styled in CSS to appear visibly (top left, with a bright color), and you can adjust the freshness duration or style as needed. In summary, the code hooks into a WooCommerce hook (
woocommerce_before_shop_loop_item_title) to dynamically enrich each product page without modifying the core of the theme.Translated with DeepL.com (free version)
Result : a stylized CSS badge visible on products less than 30 days old.
1|Information
Before using this snippet, make sure you have installed the Code Snippets plugin (free) on your WordPress site. If not, you can download it via the link below. If you have never added a specific feature to your WordPress site, we recommend you start by reading our dedicated guide (link below).
2|Prérequis
Here is the list of extensions required for this snippet to work properly. Make sure to install them before activating the snippet.
| WooCommerce | Necessary for selling virtual products on your site |
3|Snippet
Here is the snippet to use. The comments inside the code will guide you on how this snippet works.
Make sure to check the “Run snippet everywhere” box before activating the snippet.
/**
* Affiche un badge "Nouveau" sur les produits récents
*/
add_action( 'woocommerce_before_shop_loop_item_title', 'lws_afficher_badge_nouveau', 10 );
function lws_afficher_badge_nouveau() {
global $product;
// Durée pendant laquelle le produit est considéré comme "nouveau" (en jours)
$jours_nouveau = 30;
// Récupération de la date de publication du produit
$date_publication = get_the_date( 'U', $product->get_id() );
// Vérification : si le produit a moins de X jours
if ( ( time() - ( $jours_nouveau * 24 * 60 * 60 ) ) < $date_publication ) {
echo '<span class="badge-nouveau" style=" position:absolute; top:10px; left:10px; background-color:#e74c3c; color:#fff; padding:5px 10px; font-size:12px; border-radius:3px; z-index:5; ">Nouveau</span>';
}
}
Why is this code useful?
This snippet frees you from plugins like “machins-pro-badge-ultimate” that slow down your site. It gives you complete control over the appearance, duration, and display logic of the “New” badge. In short: a lightweight, elegant solution that is 100% tailored to your theme.
And above all, it flatters your visitors with the illusion of novelty—the eternal fuel of commerce.
Take it a step further
This type of customization demonstrates how WooCommerce can be extended through WordPress filters and actions. If you’d like to further customize your loyalty program, consider consulting the WooRewards documentation or hiring an experienced WordPress developer (see our Custom Development page).