Skip to content

Instantly share code, notes, and snippets.

@WPDevHQ
Last active March 18, 2016 00:32
Show Gist options
  • Save WPDevHQ/19901ab449a571686242 to your computer and use it in GitHub Desktop.
Save WPDevHQ/19901ab449a571686242 to your computer and use it in GitHub Desktop.
Display WooCommerce discount as a percentage
<?php
function themeslug_discount_product($price, $product){
global $product;
$discount = $product->sale_price ;
if ($discount) {
$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
$wc_percent = $price .' <span class="product-discount">'. sprintf( __('%s', 'themeslug' ), $percentage . '% OFF</span>' );
}
else{
$wc_percent = $price .' <span class="no_discount">'. sprintf( __('0%s', 'themeslug' ), $percentage . '% OFF</span>' );
}
return $wc_percent;
}
add_filter( 'woocommerce_sale_price_html', 'themeslug_discount_product', 10, 2 ); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment