phpcountvisual-composer

Get the discount on a product


I need to get the discount on a product. The old and the new price are in visual composer. I can get them but i can't count them. The old and the new price are integers but when i divide an old price on a new price i receive something like 60.929323229329. And then i have to round this number and subtract it from 100. However it doesn't work

<?php $x = $item['price_list_item_price2'];?>
<?php $y = $item['price_list_old2'];?>
<?php echo ((100) - ($x / $y)); ?>

The result must be for instance 39%


Solution

  • Not sure what you mean by discount if you want to see percentage it will be

    <?php 
    $new_price = $item['price_list_item_price2'];
    $old_price = $item['price_list_old2'];
    
    $percentage = (($old_price - $new_price) / $old_price) * 100;
    echo round($percentage , 2); ?>