0

I'm trying to display the discount amount as a percentage in the Woocommerce Sale Badge, I found the following code:

add_filter( 'woocommerce_sale_flash', 'wc_custom_replace_sale_text' );
function wc_custom_replace_sale_text( $html ) {
global $product;
$percentage = round( ( ( $product->get_regular_price() - $product-
>get_sale_price() ) / $product->get_regular_price() ) * 100 );
return str_replace( __( 'Sale!', 'woocommerce' ), __( '', 'woocommerce' 
).$percentage.'% OFF', $html ); }

But get this warning on variable products: Warning: Division by zero in /functions.php on line etc... pointing at this line

  $percentage = round( ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100 );

Any suggestions how I can get this to work would be very much appreciated!

Kind regards, JP

JapeNZ
  • 75
  • 10
  • 1
    You can probably find the answer here: https://stackoverflow.com/questions/42230027/adding-the-discount-percentage-to-variable-products-on-sale . I believe that you are only returning simple product prices as it is right now, and need some more arguments to return variations – Chri.s Dec 15 '17 at 07:09
  • 1
    Another option is Also this http://www.wpstuffs.com/display-product-discountoffer-percentage-bubble-woocommerce/ - however consider this Change https://www.tmdhosting.com/kb/question/how-to-show-woocommerce-discount-in-percentage/ – Chri.s Dec 15 '17 at 07:21
  • Hi Chri.s, thanks for the help buddy, all sorted :) – JapeNZ Jan 27 '19 at 10:23

0 Answers0