Add the discounted percentage or discount price to variable product price range in Woocommerce

Picture of Amitpal Singh
Amitpal Singh
February 9, 2024
				
					// Removing sale badge
add_filter('woocommerce_sale_flash', '__return_false');

// Removing archives prices
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );

// Add the saved discounted percentage to variable products
add_filter('woocommerce_format_sale_price', 'add_sale_price_percentage', 20, 3 );
function add_sale_price_percentage( $price, $regular_price, $sale_price ){
    // Strip html tags and currency (we keep only the float number)
    $regular_price = strip_tags( $regular_price );
    $regular_price = (float) preg_replace('/[^0-9.]+/', '', $regular_price);
    $sale_price = strip_tags( $sale_price );
    $sale_price = (float) preg_replace('/[^0-9.]+/', '', $sale_price);

    // Percentage text and calculation
    $percentage  = __('Save', 'woocommerce') . ' ';
    $percentage .= round( ( $regular_price - $sale_price ) / $regular_price * 100 );

    // return on sale price range with "Save " and the discounted percentage
    return $price . ' <span class="save-percent">' . $percentage . '%</span>';
}
				
			

Share this post:

How to Attribute?

Lorem ipsum is typically a corrupted version of De finibus bonorum et malorum, a 1st-century BC text by the Roman statesman and philosopher Cicero.
for Example: Website, Social Media, Blogs, ebooks , newsletter, etc.
Lorem ipsum is typically a corrupted version of De finibus bonorum et malorum, a 1st-century BC text by the Roman statesman and philosopher Cicero.
Copied!

Got a Question? Check out our FAQ Section.

Your action, our appreciation

It encourage us to give you more valuable content on website.