News:

Support the VirtueMart project and become a member

Main Menu

Show Discount as %

Started by CMYKreative, August 14, 2012, 12:45:40 PM

Previous topic - Next topic

CMYKreative

We have various products in the site with different % discounts but on the front end it shows the 'actual' GBP amount of discount and we'd like to simply show the % amount applied to it instead (i.e. show Discount: 25%). Any ideas how to do this? (see attached)

http://demo.cmykreative.com/berkeley/index.php?option=com_virtuemart&view=category&virtuemart_category_id=14&Itemid=384
http://demo.cmykreative.com/berkeley/index.php?option=com_virtuemart&view=productdetails&virtuemart_product_id=213&virtuemart_category_id=14&Itemid=384

Also, we have in the product page some custom dropdowns that apply a price adjustment. These work fine on products without any additional discounts applied, but on products that also have a % discount, we need the price adjustment to be made AFTER any discount (i.e. $119 -25% = $89.25 then add the custom price adjustment so $119 -25% = $89.25 + $5 = $94.25). Currently the price adjustment is made and then the discount is applied to the price adjustment as well as the main price.

Hope that makes sense! :)

[attachment cleanup by admin]


bytelord

Hello,

You can find here a useful example about discounts and on the specific post how to print out % discount.

http://forum.virtuemart.net/index.php?topic=96175.msg350859#msg350859

here is the code for product details page from the above thread from user inopia:

if ($this->currency->createPriceDiv('discountAmount','COM_VIRTUEMART_PRODUCT_DISCOUNT_AMOUNT',$product->prices))
{
$discount=$this->currency->formatNumber($product->prices['discountAmount']);
$pricewithtax = $this->currency->formatNumber($product->prices['basePriceWithTax']);
$total =($discount * 100) / $pricewithtax;
echo "Descuento: ".$total."%";
}


Hope it helps you out.

Regards.
Production: Joomla 2.5.8 | VM 2.0.14 | PHP 5.3.13
Testing     : Joomla 2.5.8 | VM 2.0.16 | PHP 5.3.8
Testing     : Joomla 2.5.8 |    VM 2.1   | PHP 5.3.8

- Don't Forget to mark thread as solved when it is solved!
- Please do not PM with support questions, use the forum!

CMYKreative

Thanks . . . tried it but I couldn't get it to work, simply got:

$0.00

It's not showing any % anywhere.

CMYKreative

OK, got it working (mostly) except now it's showing the discount even when there is none applied and I can't figure it out:

http://demo.cmykreative.com/berkeley/index.php?option=com_virtuemart&view=category&virtuemart_category_id=12

This is the code I have at the moment:

<div class="catProductPrice" id="productPrice<?php echo $product->virtuemart_product_id ?>">
<?php
if ($this->show_prices == '1') {
if( $product->product_unit && VmConfig::get('vm_price_show_packaging_pricelabel')) {
echo "<strong>"JText::_('COM_VIRTUEMART_CART_PRICE_PER_UNIT').' ('.$product->product_unit."):</strong>";
}


echo $this->currency->createPriceDiv('salesPrice','',$product->prices);
// echo $this->currency->createPriceDiv('discountAmount','COM_VIRTUEMART_PRODUCT_DISCOUNT_AMOUNT',$product->prices);
if ($this->currency->createPriceDiv('basePriceWithTax','COM_VIRTUEMART_PRODUCT_BASEPRICE_WITHTAX',$product->prices))
echo $this->currency->createPriceDiv('basePriceWithTax','COM_VIRTUEMART_PRODUCT_BASEPRICE_WITHTAX',$product->prices);
if (
$this->currency->createPriceDiv('discountAmount','COM_VIRTUEMART_PRODUCT_DISCOUNT_AMOUNT',$product->prices))
{
$discount=$this->currency->formatNumber($product->prices['discountAmount']);
$pricewithtax $this->currency->formatNumber($product->prices['basePriceWithTax']);
$total =($discount 100) / $pricewithtax;
echo "<div class='discountpercent'>Discount: ".$total."%</div>";
}
echo $this->currency->createPriceDiv('taxAmount','COM_VIRTUEMART_PRODUCT_TAX_AMOUNT',$product->prices);
?>

</div>
<div class="product-details button">
<?php // Product Details Button
echo JHTML::link($product->linkJText::_('COM_VIRTUEMART_PRODUCT_DETAILS'), array('title' => $product->product_name,'class' => 'catProductDetails button'));
?>

</div></div>


Can you figure out how to show the % discount + base price ONLY if there is a discount applied . . . ?

bytelord

#5
Hi,

You worked nicely :)

You just need an if statement.  For example: (for category view)

if (!empty($product->prices['discountAmount']) ) {
    echo $this->currency->createPriceDiv('basePriceWithTax','COM_VIRTUEMART_PRODUCT_BASEPRICE_WITHTAX',$product->prices);
    echo "<div class='discountpercent'>Discount: ".$total."%</div>";
    }


So you can calculate $total before that statement. That statement checks if discountAmount not Empty (that means if you have any discount) and if its true print out the basicpricewithtax and "Discount: total %"   

Hope it helps!         
Production: Joomla 2.5.8 | VM 2.0.14 | PHP 5.3.13
Testing     : Joomla 2.5.8 | VM 2.0.16 | PHP 5.3.8
Testing     : Joomla 2.5.8 |    VM 2.1   | PHP 5.3.8

- Don't Forget to mark thread as solved when it is solved!
- Please do not PM with support questions, use the forum!

CMYKreative

Sweet! I tweaked it a little (the % wasn't showing correctly) and this seems to work fine:

if (!empty($product->prices['discountAmount']) ) {
    echo $this->currency->createPriceDiv('basePriceWithTax','COM_VIRTUEMART_PRODUCT_BASEPRICE_WITHTAX',$product->prices);
if ($this->currency->createPriceDiv('discountAmount','COM_VIRTUEMART_PRODUCT_DISCOUNT_AMOUNT',$product->prices))
{
$discount=$this->currency->formatNumber($product->prices['discountAmount']);
$pricewithtax = $this->currency->formatNumber($product->prices['basePriceWithTax']);
$total =($discount * 100) / $pricewithtax;
echo "<div class='discountpercent'>Discount: ".$total."%</div>";
}
    }


Thanks!

bytelord

#7
Great! Glad i helped. You can do the same with product details page.

Take a look here that will help you edit the template files further.

https://forum.virtuemart.net/index.php?topic=97744.0
https://forum.virtuemart.net/index.php?topic=100696.0
https://forum.virtuemart.net/index.php?topic=92756.0

Best Regards,

Bytelord
Production: Joomla 2.5.8 | VM 2.0.14 | PHP 5.3.13
Testing     : Joomla 2.5.8 | VM 2.0.16 | PHP 5.3.8
Testing     : Joomla 2.5.8 |    VM 2.1   | PHP 5.3.8

- Don't Forget to mark thread as solved when it is solved!
- Please do not PM with support questions, use the forum!

CMYKreative

For those that are interested, we got it working on the products details page using this:

if (!empty($this->product->prices['discountAmount']) ) {
echo $this->currency->createPriceDiv('basePriceWithTax','COM_VIRTUEMART_PRODUCT_BASEPRICE_WITHTAX',$this->product->prices);
if ($this->currency->createPriceDiv('discountAmount','COM_VIRTUEMART_PRODUCT_DISCOUNT_AMOUNT',$this->product->prices))
{
$discount=$this->currency->formatNumber($this->product->prices['discountAmount']);
$pricewithtax = $this->currency->formatNumber($this->product->prices['basePriceWithTax']);
$total =($discount * 100) / $pricewithtax;
echo "<div class='discountpercent'>Discount: ".$total."%</div>";
}
    }


No discount % on product so % doesn't display:
http://demo.cmykreative.com/berkeley/index.php?option=com_virtuemart&view=productdetails&virtuemart_product_id=40&virtuemart_category_id=12

% discount on product, so % does display:
http://demo.cmykreative.com/berkeley/index.php?option=com_virtuemart&view=productdetails&virtuemart_product_id=170&virtuemart_category_id=11&Itemid=384

rzrz

Hi I followed but I only get

Discounted Price: $20
Discount Amount: $5

How to get

Was: $25
Now: $20 ?

bytelord

Hello,

You will use:

Baseprice with Tax, but without discounts: $25   [basePriceWithTax]
Final Sales Price: $20 [salesPrice]
Production: Joomla 2.5.8 | VM 2.0.14 | PHP 5.3.13
Testing     : Joomla 2.5.8 | VM 2.0.16 | PHP 5.3.8
Testing     : Joomla 2.5.8 |    VM 2.1   | PHP 5.3.8

- Don't Forget to mark thread as solved when it is solved!
- Please do not PM with support questions, use the forum!

rzrz

Quote from: bytelord on August 18, 2012, 07:38:31 AM
Hello,

You will use:

Baseprice with Tax, but without discounts: $25   [basePriceWithTax]
Final Sales Price: $20 [salesPrice]

Hi thanks
but i am looking for when an item is not on sale, it will display:

Price : $25

When its on discount it will display

Was: $25
Now: $20

CMYKreative

Use the code I posted above, but just change the various pricing codes to suit what you need to show.

rzrz

Quote from: CMYKreative on August 19, 2012, 13:29:13 PM
Use the code I posted above, but just change the various pricing codes to suit what you need to show.

hi sorry i m not very well versed in php but i tried this this code that you posted


<?php if (!empty($this->product->prices['discountAmount']) ) { 
echo 
$this->currency->createPriceDiv('basePriceWithTax','COM_VIRTUEMART_PRODUCT_BASEPRICE_WITHTAX',$this->product->prices);
if (
$this->currency->createPriceDiv('discountAmount','COM_VIRTUEMART_PRODUCT_DISCOUNT_AMOUNT',$this->product->prices))
{
$discount=$this->currency->formatNumber($this->product->prices['discountAmount']);
$pricewithtax $this->currency->formatNumber($this->product->prices['basePriceWithTax']);
$total =($discount 100) / $pricewithtax;
echo "<div class='discountpercent'>Discount: ".$total."%</div>";
}
    }
 
?>




enabled all prices at the backend and this is the output

Product with discounts:
Salesprice with discount: $24.00
Sales Price Without Tax: $30.00
Discount: $6.00
Sales Price: $24.00
Base price with tax: $0.00
Discount: %

Product without discounts
Sales Price Without Tax: $50.00
Sales Price: $50.00

Why base price with tax it will display as 0?  :-\

CMYKreative

Hide the divs you dont want to show with css . . . easiest way.