"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.
Opens WPIDE on
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
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.