Halloween Sale 15% off Ends in See the plans
PHP

"New" Badge for Recent Products

Shows a "New" label on products in the shop that were published in the last 30 days.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.