Last active
March 22, 2019 08:03
-
-
Save webdados/4170a4e5d44758e679703322eda0b557 to your computer and use it in GitHub Desktop.
Discount percentage on the WooCommerce "on sale" badge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?