Want to make discounted products stand out in your WooCommerce store? You can automatically display a “Sale” badge on products that are currently on sale.
Steps
- Add the code below to your theme’s
functions.php. - Save the changes.
- Any product with a sale price will automatically show the badge.
/**
* Add custom "Sale" badge to WooCommerce products
*/
function custom_woocommerce_sale_badge() {
global $product;
if ( ! $product || ! $product->is_on_sale() ) {
return;
}
echo '<span class="custom-sale-badge">Sale</span>';
}
add_action(
'woocommerce_before_shop_loop_item_title',
'custom_woocommerce_sale_badge',
5
);
