Disclosure: this article was written with the help of AI. The snippets use WooCommerce's documented hooks but are illustrative: test them on your own WooCommerce version before using them in production. It is posted by the team at AiTech.

Product badges are one of the cheaper conversion tools in a WooCommerce store, and one of the easiest to get wrong. The default "Sale!" flash tells shoppers nothing, a hard-coded "-20%" goes stale the moment someone edits a price, and on variable products the percentage depends on which variation you look at. This article shows how to compute badge values from live product data, which hooks to use for classic and block themes, and how to avoid a page full of badge JavaScript.

Compute the number from the product, never store it

A discount percentage is derived data. Store the regular and sale price (WooCommerce already does) and calculate the badge at render time.

For a simple product that is 1 - sale / regular. For a variable product, the parent has no single price: each variation can have its own. The honest label is the biggest saving available, so loop the variations:

function myshop_max_discount_percent( WC_Product $product ): int { $candidates = $product->is_type( 'variable' ) ? array_filter( array_map( 'wc_get_product', $product->get_children() ) ) : array( $product ); $best = 0; foreach ( $candidates as $p ) { if ( ! $p->is_on_sale() ) { continue; } $regular = (float) $p->get_regular_price(); $current = (float) $p->get_price(); if ( $regular > 0 && $current < $regular ) { $best = max( $best, (int) round( ( 1 - $current / $regular ) * 100 ) ); } } return $best; } 

Use "Save up to 25%" as the wording for variable products, not "25% off", because not every variation has that saving. On a large catalogue, remember that get_children() plus wc_get_product() costs queries per product on archive pages; WooCommerce's object cache makes repeated reads cheaper, but cache the computed number in a transient or product meta if you have thousands of variations per page.

Classic themes: replace the sale flash

Classic templates print the flash through the woocommerce_sale_flash filter, which receives the HTML, the post and the product:

add_filter( 'woocommerce_sale_flash', function ( $html, $post, $product ) { $pct = myshop_max_discount_percent( $product ); if ( ! $pct ) { return $html; } $label = $product->is_type( 'variable' ) /* translators: %d: percentage */ ? sprintf( __( 'Save up to %d%%', 'myshop' ), $pct ) : sprintf( __( 'Save %d%%', 'myshop' ), $pct ); return '<span class="onsale">' . esc_html( $label ) . '</span>'; }, 20, 3 ); 

Keep the onsale class so your theme's existing CSS still positions it.

Block themes: filter the block output

Block themes do not always call that filter. The Product Collection block renders a Product Sale Badge block, and WordPress lets you filter any block's final HTML with render_block_{$name}:

add_filter( 'render_block_woocommerce/product-sale-badge', function ( $content, $block ) { $product_id = $block->context['postId'] ?? 0; $product = $product_id ? wc_get_product( $product_id ) : null; if ( ! $product ) { return $content; } $pct = myshop_max_discount_percent( $product ); return $pct ? str_replace( '>Sale<', '>' . esc_html( sprintf( 'Save %d%%', $pct ) ) . '<', $content ) : $content; }, 10, 2 ); 

The exact markup of the block can change between WooCommerce versions, so test after updates and prefer a small, tolerant replacement over rebuilding the whole element.

Low-stock badges

"Only 3 left" works because it is true. Read the stock from the product and respect the store settings:

function myshop_low_stock_label( WC_Product $product, int $threshold = 5 ): string { if ( ! $product->managing_stock() || ! $product->is_in_stock() ) { return ''; } $qty = (int) $product->get_stock_quantity(); return ( $qty > 0 && $qty <= $threshold ) /* translators: %d: units left */ ? sprintf( _n( 'Only %d left', 'Only %d left', $qty, 'myshop' ), $qty ) : ''; } 

If stock is not managed, show nothing: a badge you cannot back up with data is worse than none. Print it on the same image wrapper as the sale badge so the two do not overlap.

CSS only, and no layout shift

Badges should not need JavaScript. Position them absolutely inside the product image container, give that container position: relative, and let the badge be a plain span:

.myshop-badge-wrap { position: relative; } .myshop-badge { position: absolute; top: .5rem; left: .5rem; padding: .2rem .55rem; border-radius: 999px; font: 600 .8rem/1.2 system-ui, sans-serif; background: #e11d48; color: #fff; } @media (prefers-reduced-motion: no-preference) { .myshop-badge--pulse { animation: myshop-pulse 2s ease-in-out infinite; } } @keyframes myshop-pulse { 50% { transform: scale(1.06); } } 

Wrapping the animation in a prefers-reduced-motion query keeps it accessible. Because the badge is in the HTML from the first byte, there is no flash and no layout shift.

Caching and scheduled sales

Two things bite in production. First, scheduled sales are switched on and off by WooCommerce's cron, so a full-page cache can keep serving yesterday's badge; purge the cache when woocommerce_scheduled_sales runs or set a short TTL on shop pages. Second, if you cache the computed percentage, invalidate it on woocommerce_update_product and woocommerce_update_product_variation.

If you would rather not maintain this

AiTech packaged the same ideas, with display rules (on sale, stock status, category, tag, price, product age), live variables such as {discount} and {stock}, and hooks for classic and block themes, as a free plugin called AiTech Product Badges. It is at aitech-product-badges.pages.dev. By default the plugin shows up to four badges on each product (that cap is adjustable). The snippets above work on their own without it.