What is the tutorial about?
Web Squadron explains how to add a WooCommerce new badge to your recently added products in your store using a free code snippet. The badge will be visible for a set number of days, indicating to your customers that the product is new. This is an excellent way to highlight new products and attract customer attention.
Use Cases for this Tutorial
- Online Store Owners:
If you’re running an online store and frequently add new products, this tutorial will help you highlight those new additions effectively. - Web Designers:
This tutorial can be a handy tool for web designers who want to add dynamic elements to the WooCommerce stores they design. - Marketing Professionals:
For those involved in marketing, this tutorial can help create a sense of urgency and novelty, encouraging customers to check out new products. - WordPress Developers:
Developers can use this tutorial to learn more about using code snippets and plugins in WordPress, expanding their toolkit.
Required Code
Source: Code Snippets
/** * Snippet Name: WooCommerce Show "NEW" Badge On Newly Added Products * Snippet Author: ecommercehints.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 } }