If you’re selling Knife Sharpeners, clearly the buyer not only wants to see the regular selling price and your sale price, but the percentage off the original price. Makes their decision a tiny bit easier. Knife Sharpeners at 5% off aren’t going to get people excited, but we all know that Knife Sharpeners at 77% off is going to cause some sort of riot and even a server meltdown..
This is a very simple code addition to your site, but as always, do a backup before you endeavor to change any essential site files.
OK, let’s go…
In your WordPress Dashboard, click Appearance and then Editor.
Confirm you’re editing your child theme, not the primary theme. Why? Because if you update your WooCommerce install, your code will be lost.
Select your function.php file.
Scroll to the very bottom of all the code & make a new line or 2 underneath ALL the other code in this box. DO NOT insert it in the middle of any other code. You are guaranteed to BREAK your site.
Copy and paste this entire code into your functions.php file.
// // Add save percent next to sale item prices. // add_filter( 'woocommerce_sale_price_html', 'woocommerce_custom_sales_price', 10, 2 ); function woocommerce_custom_sales_price( $price, $product ) { $percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 ); return $price . sprintf( __(' Save %s', 'woocommerce' ), $percentage . '% ' ); } // // End - save percentage off //
Click Update file and you’re done.
If you need a backout plan, check the Hack #1 post… same recovery method.
At this stage, I can’t have the % off in a different colour. It changes other text colours as well, but I am working on it…
The other thing it won’t do at this stage is show a percentage off if there is a variant – say a size or colour. I’m pretty happy with the way it works right now, I have found a possible work around, so stay tuned. Replacing the code is easy if you want to start with this version, and update it later.
Let me know what you think. Love to hear your feedback.