Last active
March 18, 2016 00:32
-
-
Save WPDevHQ/19901ab449a571686242 to your computer and use it in GitHub Desktop.
Display WooCommerce discount as a percentage
This file contains 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 | |
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