Woocommerce: Replace “Sale” with Percentage Discount

Woocommerce is a very powerful e-commerce plugins f0r WordPress. It integrate seamlessly with many plugins, payment gateway, shipping courier, and many more.

Sometimes to make the customer of the e-commerce website more willing to check the product is to give a discount price. By Default, woocommerce only show green bubble with a “Sale” text on it. I think its not that good to catch customers attention. You can change the “Sale” with the Percentage Discount with this simple code. Just put this code on your theme’s functions.php file.

add_filter( 'woocommerce_sale_flash', 'tcore_percentage_sale', 10, 3 );
function tcore_percentage_sale( $text, $post, $product ) {

	$text = '<span class="onsale">';

	$regular = $product->regular_price;
	$sale = $product->sale_price;
	if ( isset( $sale ) ) {
		$discount = ceil( ( ($regular - $sale) / $regular ) * 100 );
	}

	$text .= $discount . '%';

	$text .= '</span>';
	return $text;

}

Happy Selling!

4 Comments

  1. Ricardo-Reply
    June 27, 2017 at 6:34 pm

    Hi Eric, Thanks a lot for your code. It works on my site. I only have problem with products with variations. All variations have the same sale and regular price (reg: 200, sale: 150), but instead of the “Discount %” it shows me: “NAN%” for them.

    Do you know if it is possible to solve?

    Thanks!

  2. Rajs-Reply
    November 15, 2017 at 4:13 am

    Ricardo Did you find the solution for this?

    I get this below error on variable product
    Warning: Division by zero in /home/xxxxxxxxxxxxxxxxx-store-child/functions.php on line 16

  3. Châu-Reply
    March 24, 2018 at 9:13 am

    Thanks you so much!

  4. September 5, 2018 at 10:03 am

    Wow! Incredible Code, Eric. Seems you’re Indonesian too, Right?

    Nice to meet you, keep it up! I like your site.

Leave A Comment