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 . '%' ); | |
} | |
?> |