Halloween Sale 15% off Ends in See the plans
PHP

"Bestseller" Badge for Popular Products

Labels products that have sold at least a set number of units with a "Bestseller" badge in shop listings and on the product page.

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

Code

$wpide_bestseller_badge = function () {
    global $product;

    $min_sales = 50; // Units sold before the badge appears.

    if ( ! $product instanceof WC_Product || (int) $product->get_total_sales() < $min_sales ) {
        return;
    }

    echo '<span class="wpide-bestseller-badge" style="display:inline-block;margin:0 0 8px;padding:3px 10px;border-radius:3px;background:#dba617;color:#1e1e1e;font-size:12px;font-weight:600;text-transform:uppercase;">' . esc_html( 'Bestseller' ) . '</span>';
};

add_action( 'woocommerce_shop_loop_item_title', $wpide_bestseller_badge, 5 );
add_action( 'woocommerce_single_product_summary', $wpide_bestseller_badge, 4 );

Notes

Change $min_sales to the number of units a product must sell to earn the badge.

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.