Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mvital/c4f9e8770e8037bf4e6124c14c595c52 to your computer and use it in GitHub Desktop.
Save mvital/c4f9e8770e8037bf4e6124c14c595c52 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment