Home / Admin / Show ‘NEW’ Badges for Recently Added Items in WooCommerce
Duplicate Snippet

Embed Snippet on Your Site

Show ‘NEW’ Badges for Recently Added Items in WooCommerce

This code snippet adds a “NEW!” badge to products in WooCommerce, indicating that they are newly added. The badge is displayed both on the product archive pages and the single product template for a specified period (in this case, 10 days) after the product’s creation date.

Code Preview
php
<?php
/**
 * Snippet Name:    Show 'NEW' Badges for Recently Added Items in WooCommerce
 * Snippet Author:    wdxtechnologies.com
 */
 
// Show the NEW badge on the archive loop item
add_action( 'woocommerce_after_shop_loop_item_title', 'ecommercehints_product_archive_new_badge', 1 );
function ecommercehints_product_archive_new_badge() {
   global $product;
   $days_to_show = 10; // Show the new badge for 10 days
   $product_published_date = strtotime( $product->get_date_created() );
   if ( ( time() - ( 60 * 60 * 24 * $days_to_show ) ) < $product_published_date ) {
      echo '<span class="onsale">' . 'NEW!' . '</span>'; // The "NEW!" text with the sale badge styling
   }
}
 
// Show the NEW badge on the single product template
add_action( 'woocommerce_single_product_summary', 'ecommercehints_single_product_new_badge', 3 );
function ecommercehints_single_product_new_badge() {
   global $product;
   $days_to_show = 10; // Show the new badge for 10 days
   $product_published_date = strtotime( $product->get_date_created() );
   if ( ( time() - ( 60 * 60 * 24 * $days_to_show ) ) < $product_published_date ) {
      echo '<span class="onsale">' . 'NEW!' . '</span>'; // The "NEW!" text with the sale badge styling
   }
}

Comments

Add a Comment