News:

You may pay someone to create your store, or you visit our seminar and become a professional yourself with the silver certification

Main Menu

Almost able to display discount as "%" in the shop.. but not quite

Started by Genius WebDesign, May 27, 2012, 22:46:06 PM

Previous topic - Next topic

Genius WebDesign

I'm trying to display the discount of the products in the category view as "%" instead of the normal discount amount, and I feel that I'm very close to achieving this..

In the default.php file located in the category folder in my templates folder I simply added this function;



<?php

$mysalesprice = $this->currency->createPriceDiv('salesPrice','COM_VIRTUEMART_PRODUCT_SALESPRICE',$product->prices);
$normalmarketprice = $this->currency->createPriceDiv('basePriceWithTax','COM_VIRTUEMART_PRODUCT_BASEPRICE_WITHTAX',$product->prices);

$newfunc = create_function('$mysalesprice,$normalmarketprice', 'return "You are saving: " . (100-($mysalesprice / $normalmarketprice * 100));');

echo $newfunc($mysalesprice,$normalmarketprice) . "%\n";

?>



The following is displayed in front end:

"Warning: Division by zero in /home/x/n/ftp_xn--tr-og-byg-h3adk/templates/explore/html/com_virtuemart/category/default.php(278) : runtime-created function on line 1
You are saving: 100% "


The problem is that the line:

$this->currency->createPriceDiv('salesPrice','COM_VIRTUEMART_PRODUCT_SALESPRICE',$product->prices);

echoes the price in numbers plus the currency text.. This function of course does not accept text, so I need to somehow make the int $mysalesprice = price-only (without any text)..
I have tried to simply remove the currency text in the currency settings from virtuemart backend but that doesnt work.

Does anyone know how to simply get the int-value of the price so that this function can work?
It feels like this is a very basic thing, and if only I werent a complete noob this wouldn't be a problem..

Brgds





Genius WebDesign

To echo the discount amount in "%" you simply apply this code (this goes for the product details page, if you want to echo this in category browse etc. you must use $product->prices instead of $this->product->prices) :

<?php
$discountpercentage = ($this->product->prices['discountAmount'] /  $this->product->prices['basePriceWithTax']) * 100;
echo "You save ", number_format($discountpercentage, 0).'%';
?>   

Setko

Hey,

I've been able to get discount amount in % with this code that you gave. But if there is no discount for some article, it still says that it is 0% of discount. I've been trying with display: none, if there is no discount, but i didn't have any luck with it.