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