Display the default discounted price and percentage on Woocommerce products

Display the default discounted price and percentage on Woocommerce products

I am trying to display the percentage discount of a product on Woocommerce. The solution originally provided (linked below) works, however the discount percentage doesn't display if there is a default product variation set. Only when the selection is changed to another variation does the percentage discount appear. How would I modify the code to display the percent discount immidiately - without having to select another variation?

I am trying to display the percentage discount of a product on Woocommerce. The solution originally provided (linked below) works, however the discount percentage doesn't display if there is a default product variation set. Only when the selection is changed to another variation does the percentage discount appear. How would I modify the code to display the percent discount immidiately - without having to select another variation?

The original solution: Display the discounted price and percentage on Woocommerce products

2) The saving percentage:

add_filter( 'woocommerce_get_price_html',     'change_displayed_sale_price_html', 10, 2 );
function change_displayed_sale_price_html( $price, $product ) {
     // Only on sale products on frontend and excluding min/max price on variable products
    if( $product->is_on_sale() && ! is_admin() && ! $product->is_type('variable')){
         // Get product prices
         $regular_price = (float) $product->get_regular_price(); // Regular price
         $sale_price = (float) $product->get_price(); // Active price (the "Sale price" when on-sale)

     // "Saving Percentage" calculation and formatting
     $precision = 1; // Max number of decimals
     $saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), 1 ) . '%';

     // Append to the formated html price
     $price .= sprintf( __('<p class="saved-sale">Save: %s</p>', 'woocommerce' ), $saving_percentage );
}
return $price;

}


php wordpress

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Add a PHP page to WordPress

WordPress is one of the best blogging platforms. This is the scenario, we have a custom PHP page which we want to integrate with the WordPress blog. Our PHP file which we are getting to integrate might not use any of the WordPress APIs. Integrating PHP files with WordPress is very important for the back-end development of web design.

Hire Dedicated Wordpress Developer

Find the latest jobs available in The Middle East today.

Hire Dedicated PHP Developer

Looking to hire affordable yet experienced PHP developers? **[Hire Dedicated PHP Developer](https://hourlydeveloper.io/hire-dedicated-php-developer/ "Hire Dedicated PHP Developer")**, who can convert your idea to reality, within the stipulated...

PHP Web Application Development Company

As a top **PHP Web Application Development Company in USA**[https://www.dataeximit.com/php-development-services/](https://www.dataeximit.com/php-development-services/ "https://www.dataeximit.com/php-development-services/"), we at Data EximIT have...

How to use highchart in php with example

Here, i will let you know that how to use highchart in php application with example. Highchart provides feature to draw different type of charts.