When you have items on sale in WooCommerce, by default WooCommerce will display the regular price striked out with the sale price next to it.
Why not take it a step further and show your customers the savings they are getting on the sale price, with this snippet of code you can easily display the percentage saved next to the price of items on sale in WooCommerce.
Place the code below in your theme’s functions.php file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// 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 . '%' ); | |
} | |
?> |
Amazing! Thanks for this useful function.
Awsome solution!!
I’m glad i’ve found your site. You got some amazing solutions voor Woocommerce!
I might even have a challange for you. ^^
I’ve bin looking for weeks for a plugin or a solution to add delivery days to the single product page.
I want to display: “2/3 workdays delivery time”
and i’m looking for a solution 2 add excluding tax price like so:
€121 (including tax)
€ 100 (excluding tax) <- displays smaller under the Including price.
Maybe you got an idea about those 2 options??
Hi Nigel
For the delivery days you should be able to use the WooCommerce Product Add-ons extension http://www.woothemes.com/products/product-add-ons/
As for the price incl and excl tax, this is a bit difficult or not possible at all because what tax rate do you apply to customers as taxes are based on customer location and that is only known at checkout when the customer enters their address details.
Hi Gerhard,
Somehow the word “Save” does not show on the Home page but only the product page. For example, Home page products show Price $ and % Amount right next to it, no spacing in between but the product page shows Price $ Save % Amount – correctly.
How do I add the “Save” and leave a space in between the Price and Save %? Here is the site.
http://www.zenorganichealth.com.au
Thank you,
Rose
Hi Gerhard,
Thanks for your reply!.
As for the Product add-ons, for this to wok you need 2 add per product. My intension is to BULK add the option, and it needs to display different delivery time as the product goes in and out of stock. so IN stock items 2-3 days delivery and OUT of stock 5-7 days delivery. and to mangae that over 300 items is a lot of work if i have 2 do this 1 at the time.
As for the incl and excluding tax. the excluding price is shown in the cart widget why cant i display that on the produt page aswell. cause i only use 1 Country for delivery and purchase. I’m not shipping all over the world, just the Netherlands. Ill have to go look ferther then.
anywayz thanx for the reply.
Hello Gerhard,
This code worked fine in the previous WordPress/Woocommerce versions. Since the updates adding this code makes the whole site go blanc. Is there another code for this so it can function again in the most recent versions please?
Thx,
Annie
Hi Annie
Are you still having problems with this?
Hello Gerhard,
I tried this code and Annie’s below which includes variable products (I gather).
I get a white screen.
I can see the logic of code, but am certainly not a coder, can you help?
I’d really love to get this functionality for all product types (variable included) for my site.
I am also looking for a way to sort by % off, is there an easy way to add this to the sorting dropdown in woocommerce?
Thanks, Sam
Hi Gerhard,
Thx for asking!
Code works fine, after some troubleshooting, I noticed I copied ä little “extra” that did not belong in the code, what caused it to not work. This code below still works:
// 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( __(‘ – %s’, ‘woocommerce’ ), $percentage . ‘%’ );
}
add_filter( ‘woocommerce_variable_sale_price_html’, ‘woocommerce_custom_variable_sales_price’, 10, 2 );
function woocommerce_custom_variable_sales_price( $price, $variable ) {
$reg_price = get_post_meta( $variable->children[0], ‘_regular_price’, true );
$sale_price = get_post_meta( $variable->children[0], ‘_sale_price’, true );
$percentage = round( ( ( $reg_price – $sale_price ) / $reg_price ) * 100 );
return $price . sprintf( __(‘ – %s’, ‘woocommerce’ ), $percentage . ‘%’ );
}
Annie
Annie I tried to use your variable code but this line is giving me an error (Parse error: syntax error, unexpected T_STRING) . Any ideas why ?
$percentage = round( ( ( $reg_price – $sale_price ) / $reg_price ) * 100 );
Ok I got this working as well . It seems to e something to to with copy and paste . Only problem is when I show ‘Top Rated Products ‘ or ‘Products ‘ using widgets it gives
Warning: Division by zero in
and % amount does not show…where as the same product in detail view, warning does not show and every thing looks fine including the percentage amount correctly calculated
Your comment is awaiting moderation.
Hello, I made a web site and I used this wonderful plugin for an on line store. Now I need to know what is the best way to change the price off all product, we are in Canada and we sell the books and the price can change according to the change, for example next month I need to change all price plus 1.5%. Some one knows easy way to change the price @ the same time?
Thank you
Hi Christian
There is a bulk edit option on the edit product list page, select all the products and choose edit from the dropdown to go into bulk edit mode. From there you can update the prices for all products with a single entry.
This is great thank you! Is there a variation to show the dollar amount saved ?
Hi Gerhard,
This is fantastic, thanks!
Is there another code to show the dollar amount saved rather than the percentage?
Rebecca.
Hi Rebecca
For that you will need to use the following code
add_filter( 'woocommerce_sale_price_html', 'woocommerce_custom_sales_price', 10, 2 );
function woocommerce_custom_sales_price( $price, $product ) {
$saved = wc_price( $product->regular_price - $product->sale_price );
return $price . sprintf( __(' Save %s', 'woocommerce' ), $saved );
}
Hi Gerard.
I am looking for a way to add this calculation as a custom field so that I can sort by percentage saved. Any ideas on getting woicommerce to sort by savings?
Thanks
Sam
Good job , man !
Ask a Question , How to add hour in the sale price dates?
Accurate to hours
Hi
This isn’t working for me at all. I have the WordPress 3.9.1.
I also use variations. I have pasted your code in the functions.php in my child theme and had no effect.
can you please assist?
I did subscribe 🙂
Ok I got it to work. 🙂
Thank you so much for putting together these in-depth tutorials. I’ve found them to be very helpful.
I am interested in using the “Sale” bubble to display a numbered list of the products in any given category.
With this post, I see that you are somehow manipulating the sale bubble, but I’m curious if it would be easy to generate an automatic number list for any set category of products and display those numbers in the “sale bubble” or at least tie a custom text field to it.
Something like this is what I’m hoping to achieve:
http://collegetropolis.com/wp-content/uploads/numbered-products.png
Any help would be greatly appreciated. Thanks, again.
Hi Gerhard,
I cannot get this to work.
I don’t get any errors but it just does not show up. Is there any CSS styling i have to do? I have latest woocommerce (version 2.1.12).
Thank you,
Ana
Hi Gerard,
I found your posts very useful to everyone… it saves a lot of time… thank you..
Also, how can I position an action or filter to a certain area in the shop page? single product page?
Hi there,
I have been able to use the save % off code ( kindly provided by Gerhard ) for some time, but it got broken after the big update of Woocommerce. If you don’t mind Gerhard? I would like to point out an affordabel plugin that works great even after the big update: http://club.orbisius.com/products/wordpress-plugins/woocommerce-extensions/orbisius-woocommerce-ext-percent-off/
I read some of you are looking for more options to bulk save % off and I am using this one now and find it very easy to use as well: http://club.orbisius.com/products/wordpress-plugins/woocommerce-extensions/orbisius-woocommerce-ext-price-changer/
Hope this helps some of you who are still looking as I was a while ago. 🙂
Kind regards,
Annie
I just tried to install this code into my shop and the whole website went down, with the inability to even access the backend. I’m running WordPress 4.0 and Woocommerce 2.2.2
After freaking out for a thankfully short 5 minutes, I uploaded a backed up copy of my themes functions.php file, and everything was back to normal.
Is this a bug in the code? an incompatibility problem? Any way around it?
Michael, code works fine. If your theme already has brackets, then omit that part and simply add the code between it.
That worked a treat, thank you very much for your help!
I am using Dynamic pricing plugin and wanted to know if you could get the discount to show just like you do in your example here.
Hi Gerhard,
Can you advise on the correct method to add a css class to this savings percentage.
I would like to custom style this element.
Many thanks
Chris
Full disclosure: I am a total noob^^
I added your snipped to the functions.php of my child theme. However, it suprisingly broke my page completely! I could not even access the admin-panel anymore. This is the beginning of the error message: “Parse error: syntax error, unexpected ‘<'"
I restored the functions.php via FTP so things are fine now again. Any idea where I might have messed up? I just copied and pasted your code.
Hi, omit the when pasting it into your functions.php file, your file most probably already have that there.
Awesome! Thanks for this snippet, it worked perfect!
Hi Gerhard,
Do you have an idea how to display saved percentage in variable products?
Regards,
Elvin
Hi Gerhard!
This function is working at Single Page but it’s not working on Category Pages.
Do you have any idea why??
Thank you!
this code is not worked for variable product please help me i want to display discount for variable product
My friend Gerhard, thank you so much….it took me some time to get it right but i finally got there man, and it looks super!!!
Happy New Year
Thank you so much for this tutorial. I have already used it in a Woo project and I got another project where I could use this code if I could adjust it a bit. I hope it is not cheeky to ask but how would I go about using this tutorial but instead of deducting the $product->sale_price then deduct the $RRP from your other tutorial http://gerhardpotgieter.com/2013/09/17/woocommerce-custom-product-fields/
Thank you
Susan
Hi Guys,
Please,
I want to view the save percentage on my product description page only.
How can i do that?
Best regards
A.J.
Awesome job!
Is it possible to append ‘Save #%’ between ‘Regular Price’ and ‘Sale Price’?
Regards,
Supera.
Hey!
That’s Amazing code
It’s 100% working
Thank you
Hi Gerhard,
Thank you for this script it works perfectly.
Is it possible to add a div tag, so I can style it.
Thanks,
Camron
How to make this works with WooCommerce Subscriptions? Assuming i use variation subscription (Monthly and Yearly subcription). So Yearly subscription will show some savings.
Thanks a lot Gerhard
For the css styling, I propo this :
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 . ‘% ‘ );
}
For the css styling, I propose this :
// 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( __(‘ Économisez %s’, ‘woocommerce’ ), $percentage . ‘% ‘ );
}
This isnt working with the latest WooCommerce update.
I have used this snippet before with succes (on a elder Woocommerce version) – but this time using the latest WooCommerce, i get this error:
Warning: Missing argument 2 for woocommerce_custom_sales_price() in /path-to-template/functions.php on line 800
Warning: Division by zero in /path-to-template/functions.php on line 801
The code is located between line 799 and 803.
As i dont know what the error is, i can’t modify the code to fit…
how to insert discount in database
0 Pingbacks
Categories
Top Posts & Pages
Instagram
No Instagram images were found.
Get Email Notifications