WooCommerce's sale badge only says "Sale". Showing the saving instead, such as "SALE 25% off", tells shoppers how good the deal is before they click. On WooCommerce 11.3 or later, block themes can do this with a block setting. Classic themes need the short snippet below, which also covers grouped products and works in block themes too. It works out the percentage for every product, so there is nothing to enter per product.
On a classic theme, the snippet gives you badges like these:



Block themes: a setting in WooCommerce 11.3+
WooCommerce 11.3 added discount options to the On-Sale Badge block (11.3.0 release notes). In a block theme such as Twenty Twenty-Five:
- Go to Appearance > Editor > Templates and open Product Catalog. Products by Category and Products by Tag use the Product Catalog design until you customise them; Product Search Results has its own template, so change that one too.
- Select the On-Sale Badge block on a product image.
- In the block settings, under Badge content, set Show to Percentage. Add a prefix such as "SALE " or a suffix such as " off" if you want words around the number.
- Save.
Variable products whose variations have different discounts show "Up to 40%". Grouped products keep the plain "Sale" text, because a grouped product has no price of its own.
WooCommerce's default Single Product template still shows the badge through the older Product Image Gallery block, which prints the classic badge. The block setting doesn't reach it, but the snippet below does.
The snippet for classic and block themes
Add this to your child theme's functions.php or to a code snippets plugin such as Code Snippets. Don't put it in the parent theme's file: the next theme update overwrites it.
/**
* Highest discount on a product as a whole percentage, or 0 when there is none.
* Works for simple, variable and grouped products.
*/
function wf_discount_percentage( $product ): int {
if ( ! $product instanceof WC_Product ) {
return 0;
}
$highest = 0;
// Grouped: use the biggest discount among the child products.
if ( $product->is_type( 'grouped' ) ) {
foreach ( $product->get_children() as $child_id ) {
$child = wc_get_product( $child_id );
if ( $child && ! $child->is_type( 'grouped' ) && $child->is_on_sale() ) {
$highest = max( $highest, wf_discount_percentage( $child ) );
}
}
return $highest;
}
// Pairs of regular price and current price.
$pairs = array();
if ( $product->is_type( 'variable' ) ) {
$prices = $product->get_variation_prices();
if ( is_array( $prices ) && ! empty( $prices['price'] ) ) {
foreach ( $prices['price'] as $variation_id => $price ) {
$pairs[] = array( $prices['regular_price'][ $variation_id ] ?? 0, $price );
}
}
} else {
$pairs[] = array( $product->get_regular_price(), $product->get_price() );
}
foreach ( $pairs as list( $regular, $price ) ) {
$regular = (float) $regular;
$price = (float) $price;
if ( $regular > 0 && $price < $regular ) {
$highest = max( $highest, (int) round( ( $regular - $price ) / $regular * 100 ) );
}
}
return $highest;
}
/**
* Badge text, for example "SALE 25% off".
*/
function wf_sale_badge_label( int $percentage ): string {
/* translators: %d: discount percentage. */
return sprintf( __( 'SALE %d%% off', 'wf' ), $percentage );
}
/**
* Classic templates: the sale flash on shop, product and related product listings.
*/
add_filter( 'woocommerce_sale_flash', 'wf_sale_flash_percentage', 20, 3 );
function wf_sale_flash_percentage( $html, $post, $product ) {
$percentage = wf_discount_percentage( $product );
if ( $percentage < 1 ) {
return $html; // Keep the default badge.
}
return '<span class="onsale">' . esc_html( wf_sale_badge_label( $percentage ) ) . '</span>';
}
/**
* Block templates: the On-Sale Badge block and the Product Image block's badge (WooCommerce 10.0+).
*/
add_filter( 'woocommerce_sale_badge_text', 'wf_sale_badge_text_percentage', 10, 2 );
function wf_sale_badge_text_percentage( $sale_text, $product ) {
$percentage = wf_discount_percentage( $product );
return $percentage > 0 ? wf_sale_badge_label( $percentage ) : $sale_text;
}What each part does:
wf_discount_percentage()compares the regular price with the current price. Variable products use the biggest saving across their variations. Grouped products use the biggest saving among their child products, variable children included.- Products with a regular price of 0 or no price at all are skipped, so PHP 8 never divides by zero or calls
max()on an empty list. When there is no usable percentage, both filters keep WooCommerce's normal badge. - The
woocommerce_sale_flashfilter changes the badge in classic PHP templates: shop, category, product page and related products. It also changes the product page badge in WooCommerce's default block template. - The
woocommerce_sale_badge_textfilter, added in WooCommerce 10.0, changes the text of the On-Sale Badge block and of the badge built into the Product Image block (source). It returns plain text, which WooCommerce escapes.
The label is a translatable string with a placeholder, so a translation can put the number wherever the language needs it. Replace wf with your child theme's text domain if you translate the site. If your variations have different discounts, you may prefer 'Up to %d%% off', since the badge shows the biggest saving.
Style the badge
In block themes, style the On-Sale Badge block in the Site Editor (colour, typography and border). In classic themes the badge uses the onsale class. WooCommerce's default stylesheet draws it as a small circle, which is too narrow for a longer label. This CSS turns it into a pill; add it in Appearance > Customize > Additional CSS:
.woocommerce span.onsale {
min-width: 0;
min-height: 0;
padding: 0.3em 0.8em;
line-height: 1.6;
border-radius: 999px;
}Many themes style the badge their own way, so check yours before and after.
For a badge on new arrivals, see how to show a NEW badge on recent products. If you want badges tied to your own pricing rules, our WooCommerce developers can build them.