Skip to content

Instantly share code, notes, and snippets.

@webdados
Last active March 22, 2019 08:03
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save webdados/4170a4e5d44758e679703322eda0b557 to your computer and use it in GitHub Desktop.
Save webdados/4170a4e5d44758e679703322eda0b557 to your computer and use it in GitHub Desktop.
Discount percentage on the WooCommerce "on sale" badge
<?php
add_filter( 'woocommerce_sale_flash', 'percentage_woocommerce_sale_flash', 10, 3 );
function percentage_woocommerce_sale_flash( $html, $post, $product ) {
if ( $html!='' ) {
$perc = round( 100 - ( $product->sale_price * 100 / $product->regular_price ) );
if ( $perc>0 ) $html = '<span class="onsale">-'.$perc.'%</span>';
}
return $html;
}
@Jeppeskovsgaard
Copy link

This works fine, however with WP_debug turned on, I get this error:

"Notice: sale_price was called incorrectly. Product properties should not be accessed directly."

What is the proper way to call the sale and regular price?

@ailism
Copy link

ailism commented Aug 11, 2017

where can i add this code ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment