* Change sale badge to percent calculation.
 * @param string $html    current html badge code.
 * @param object $post    current post object.
 * @param object $product current product object.
 * @return string
function dorzki_change_badge_to_percentage( $html, $post, $product ) {

	$price = intval( ( $product instanceof WC_Product_Variable ) ? $product->get_variation_regular_price() : $product->get_regular_price() );
	$sale = intval( ( $product instanceof WC_Product_Variable ) ? $product->get_variation_sale_price() : $product->get_sale_price() );

	$percent = floor( 100 - ( $sale * 100 / $price ) );

	return sprintf( '<div class="onsale-wrapper"><span class="onsale">%s%% %s</span></div>', $percent, __( 'Off', 'woocommerce' ) );


add_filter( 'woocommerce_sale_flash', 'dorzki_change_badge_to_percentage', 10, 3 );

Credit: dorzki.co.il

How to use it?

Usually adding functionality to a WordPress site is by creating a plugin.
However you can simply copy the code and paste it in your child theme’s functions.php file at the end of the file, just before ?>.

For JavaScript code you will need to add them to your theme’s main JavaScript file.

Having issues with the code?

If the code doesn’t work or you get an error please let us know in the comments section.
We will do our best to fix it as-soon-as-possible and update the code on this page.


  1. Alon

    Amazing, thanks!


Leave a Reply