0

I want to show the discount sale percentage under the prices (regular and sale prices).

There is a very close solution to that in the post:

Display the discounted percentage near sale price in Single product pages for WC 3.0+

But the problem is that it changes the price. Before the code appears like this:

enter image description here

And after the code like this:

enter image description here

As you can see the decimals disappeared and the percentage is not correct as it should show 15% Price 55€-15% off --> 46,75€

The code used:

add_filter( 'woocommerce_format_sale_price', 'woocommerce_custom_sales_price', 10, 3 );
function woocommerce_custom_sales_price( $price, $regular_price, $sale_price ) {
    // Getting the clean numeric prices (without html and currency)
    $regular_price = floatval( strip_tags($regular_price) );
    $sale_price = floatval( strip_tags($sale_price) );

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

    return '<del>' . wc_price( $regular_price ) . '</del> <ins>' . wc_price( $sale_price ) . $percentage_txt . '</ins>';
}

My expected result would be something like this, but can't find the way to do it.

enter image description here

I've been reading a lot of posts about it but couldn't find the right solution. My apologies if I duplicate the post. Thank you

Update 08/24/2019 Solved

I finally solved the problem and got the result as i wanted after some research in toher websites. I leave here the code i used so anyone can use it too.

add_action( 'woocommerce_single_product_summary',                 
'porcentaje_ahorro_ficha_producto', 12 );

function porcentaje_ahorro_ficha_producto() {
   global $product;
   if ( ! $product->is_on_sale() ) return;
   if ( $product->is_type( 'simple' ) ) {
      $max_percentage = ( ( $product->get_regular_price() - $product-        
>get_sale_price() ) / $product->get_regular_price() ) * 100;
   } elseif ( $product->is_type( 'variable' ) ) {
      $max_percentage = 0;
      foreach ( $product->get_children() as $child_id ) {
         $variation = wc_get_product( $child_id );
         $price = $variation->get_regular_price();
         $sale = $variation->get_sale_price();
         if ( $price != 0 && ! empty( $sale ) ) {
             $percentage = ( $price - $sale ) / $price * 100;
         }
         if ( $percentage > $max_percentage ) {
            $max_percentage = $percentage;
         }
      }
   }
   if ( $max_percentage > 0 ) echo "<div class='porcentaje-ahorro'>-" .     
round($max_percentage) . "%</div>"; 
}

Then I did some css and that's all. thank you all for your support.

Mikebcn
  • 9
  • 4
  • I have [updated my answer code](https://stackoverflow.com/questions/43757920/display-the-discounted-percentage-near-sale-price-in-single-product-pages-for-wc/43771188#43771188) to avoid the rounding problems you reported. Now to get the output you want, you need to use another hook and search a bit more… – LoicTheAztec Aug 20 '19 at 13:32
  • Thank you @LoicTheAztec!! now the price is showed with decimals!, the only thing I don't understand is the percentage showed, it shows 16% but its 15%.. [percentage](http://prntscr.com/ov65od) I can't post on the other question because i'm new so posted here the comment. – Mikebcn Aug 20 '19 at 14:58
  • Sorry but the code works perfectly now… I have tested with hardcoded prices: `$regular_price = 55;` and `$sale_price = 46.75;` …and I get `15%` **but not `16%` as before**! … see [this screenshot](https://i.stack.imgur.com/UNi2p.png) as a proof… So there is something else that is making an issue in your case and nobody can't help more than that. – LoicTheAztec Aug 20 '19 at 15:20
  • Thanks a lot! I really appreciate it! – Mikebcn Aug 20 '19 at 15:29
  • Please **do not edit your question with a solution**, but rather create an answer to your own question. – zx485 Aug 24 '19 at 20:32
  • So sorry @zx485 couldn't find that option. Thank you i'll do the next time. – Mikebcn Aug 28 '19 at 14:52

1 Answers1

0

You can add the follows code snippet with exact prices -

function add_discount_woocommerce_format_sale_price( $price, $regular_price, $sale_price ){
    $percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';

    $price = '<del>' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . '</del> <ins>' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price ) . '</ins> <sup>' . $percentage . __( ' save', 'text-domain' ) . '</sup>';
    return $price;
}
add_filter( 'woocommerce_format_sale_price', 'add_discount_woocommerce_format_sale_price', 99, 3 );

But if you want the exact same styling then you have to do some styling over there.

itzmekhokan
  • 2,597
  • 2
  • 9
  • 9
  • Thank you for the quick answer, but i get the error: Warning: A non-numeric value encountered in ........../snippet-ops.php(361) : eval()'d code on line 2 – Mikebcn Aug 20 '19 at 11:53
  • Please double check that error is coming due to the above code or not because I'm not getting any kind of error. – itzmekhokan Aug 20 '19 at 12:00
  • I'm using Code Snippets plugin and adding the code as posted. see the image. [image code](http://prntscr.com/ov3hyg) is it because the code isn't in functions.php? – Mikebcn Aug 20 '19 at 12:22