Created
May 16, 2014 16:04
-
-
Save arioch1984/2aae76e96f5b7407a908 to your computer and use it in GitHub Desktop.
WP filter price for add discount as percentage
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_get_price_html', 'wpa83367_price_html', 100, 2 ); | |
function wpa83367_price_html( $price, $product ){ | |
preg_match_all('!\d+\,*\d*!', $price, $prices); | |
$discount = ''; | |
if(isset($prices[0]) && !empty($prices[0])){ | |
if(count($prices[0])==2){ //There's total and discount prices | |
$total_price = floatval(str_replace(',', '.', $prices[0][0])); | |
$discount_price = floatval(str_replace(',', '.', $prices[0][1])); | |
$discount_amount = $total_price - $discount_price; | |
$discount_percentage = round(100/($total_price / $discount_amount)); | |
$discount =' <span class="discount">sconto del <span class="amount">'.$discount_percentage.'%</span></span>'; | |
} | |
} | |
return $price.$discount; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment