arioch1984
5/16/2014 - 4:04 PM

WP filter price for add discount as percentage

WP filter price for add discount as percentage

<?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;
}
?>