Promotional prices: How to view the saved price & saved percentage?

In WooCommerce a display of the saved price is missing by default, if a promotion price was deposited. With the following code which is stored in the functions.php, the saved price, including percentage can be displayed in the category view, as well as the product details.

function ts_you_save() {
  
  global $product;
  
   if( $product->is_type('simple') || $product->is_type('external') || $product->is_type('grouped') ) {
      
     	$regular_price 	= get_post_meta( $product->get_id(), '_regular_price', true ); 
        $sale_price 	= get_post_meta( $product->get_id(), '_sale_price', true );
     
     	if( !empty($sale_price) ) {
  
              $amount_saved = $regular_price - $sale_price;
              $currency_symbol = get_woocommerce_currency_symbol();

              $percentage = round( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 );
              ?>
              <p class="you_save_price">Du sparst <?php echo number_format($amount_saved,2, '.', '')."€  (". number_format($percentage,0, '', '')."%)"; ?></p>						
              <?php		
        }
   }
}

add_action( 'woocommerce_single_product_summary', 'ts_you_save', 15 );
add_action( 'woocommerce_after_shop_loop_item', 'ts_you_save', 5 );
Without cookies
This website does not use cookies or tracking. More information can be found in the privacy policy.