News:

Support the VirtueMart project and become a member

Main Menu

Displaying discount (in %) in template

Started by chevyvan79, July 16, 2012, 15:13:51 PM

Previous topic - Next topic

chevyvan79

Hi,

It's day 15, I'm running low on resources. I've crawled the entire internet for a possible solution with no success, yet.

I'm creating a template for VirtueMart v2.08c and I want to display the discount (in %) of a product, like a big label saying: "-50%".

Does anyone know if that's possible and how it can be done?

Thanks in advance.

quimkaos

and... another one that i'm trying to do... again... does anyone knows any workaround for this?

inopia

Ei, in this thread i´ve post the way to do % discount.

http://forum.virtuemart.net/index.php?topic=96175.45

but tomorrow i will post an efficient way to do this :)

chevyvan79

Thanks inopia,

I had a look at your code and It's actually 1 simple calculation for the discount. Just calculating the discount on the fly. :) Simple, but effective.

I'm going to use your solution.

Thanks, again. :D

quimkaos

thanks!!!

i'm going to test this later on!

chevyvan79

Hi guys,

I found another solution by myself. All the discount of a product information is stored in the $product->prices['DATax'] array.

Obtaining the discount information of a product:
Product Discount Rule Name: $product->prices['DATax'][0][0]
Product Discount Value: $product->prices['DATax'][0][1]
Product Discount Math Operation (like "-%"): $product->prices['DATax'][0][2]

The product discount value will return something like "25.970", if you want to display this as "-26%", you can use this line of code:
echo '-' . round( $product->prices['DATax'][0][1] ) . '%';

I hope this helps!  :D

chevyvan79

Hi again,

As addition to my last reply I want to add that there are 2 types of discounts.

DBTax: Discount Before Tax: $product->prices['DBTax']
DATax: Discount After Tax: $product->prices['DATax']

But, I guess half of you already knew. :P

inopia

#7
Well. I promise a better solution.

First, changes on administrator/components/com_virtuemart/helpers/currencydisplay.php


/**
* function to create a div to show the prices, is necessary for JS
*
* @author Max Milbers
* @author Patrick Kohl
* @param string name of the price
* @param String description key
* @param array the prices of the product
* return a div for prices which is visible according to config and have all ids and class set
*/
public function createPriceDiv($name,$description,$product_price,$priceOnly=false,$switchSequel=false,$quantity = 1.0){

// vmdebug('createPriceDiv '.$name,$product_price[$name]);
if(empty($product_price)) return '';

//The fallback, when this price is not configured
if(empty($this->_priceConfig[$name])){
// echo 'createPriceDiv empty($this->_priceConfig[$name] '.$name.'<br />';
//This is a fallback because we remove the "salesPriceWithDiscount" ;
$name = "salesPrice";
if(is_array($product_price)){
$price = $product_price[$name] ;
} else {
$price = $product_price;
}
} else {
$price = $product_price[$name] ;
}
/* if(!is_double($price)){
// vmdebug('Price is not double? ',$price);
$price =  'Price is not double? '.$name.'<pre>'.print_r($price,1).'</pre>';
} else {
$price = $price * (float)$quantity;
}*/

//This could be easily extended by product specific settings
if(!empty($this->_priceConfig[$name][0])){
if(!empty($price)){
$vis = "block";
$priceFormatted = $this->priceDisplay($price,0,(float)$quantity,false,$this->_priceConfig[$name][1] );
} else {
$priceFormatted = '';
$vis = "none";
}
if($priceOnly){
return $priceFormatted;
}
$descr = '';
if($this->_priceConfig[$name][2]) $descr = JText::_($description);
// vmdebug('createPriceDiv $name '.$name.' '.$product_price[$name]);

// ==
// Añadimos código
// ==

// Comprobamos que sea un descuento
if ($name == "discountAmount" && $vis == "none")
{
// Si el descuento está vacío (discountAmount) (display: none). Obtenemos una cadena vacía en lugar de un div
return "";
}

// Si hay descuento y queremos mostrar el precio con impuestos (PriceWithTax)
if ($name == "basePriceWithTax")
{
// Comprobamos que haya descuento
if(!empty($product_price['discountAmount']))
{
$vis = "block";
//Vamos a darle formato al precio
$priceFormatted = $this->priceDisplay($price,0,(float)$quantity,false,$this->_priceConfig[$name][1] );

// Existe descuento y mostramos el precio con impuestos (PriceWithTax) y aplicamos los nuevos estilos
$css = "red";
return '<div class="Price'.$name.'" style="display : '.$vis.';" >'.$descr.'<span class="Price'.$name.' '.$css.'" >'.$priceFormatted.'</span></div>';
}
else {
$priceFormatted = '';
$vis = "none";
return '<div class="Price'.$name.'" style="display : '.$vis.';" >'.$descr.'<span class="Price'.$name.' '.$css.'" >'.$priceFormatted.'</span></div>';
}
}

// ==
// Fin código
// ==

if(!$switchSequel){
return '<div class="Price'.$name.'" style="display : '.$vis.';" >'.$descr.'<span class="Price'.$name.'" >'.$priceFormatted.'</span></div>';
} else {
return '<div class="Price'.$name.'" style="display : '.$vis.';" ><span class="Price'.$name.'" >'.$priceFormatted.'</span>'.$descr.'</div>';
}
}

}


Second, add a new function to currencydisplay.php

/**
*
* @author inopiaDesign
* @param number $discount
* @param number $price
*/
function createDiscountDiv($name,$descr,$discount,$price){
$descripcion = JText::_($descr);
$css= "discount";
return '<div class="Price'.$name.'">'.$descripcion.'<span class="Price'.$name.' '.$css.'" >'.($discount*100)/$price.'%'.'</span></div>';
}


Well, we´ve finished with currencydisplay.php

We start with template views:

Category View: templates/your_template_name/html/com_virtuemart/category/default_products.php

Go to the Output Price DIV


<?php // Output product Price
if ($this->show_prices == '1') { ?>

<div class="product-price" id="productPrice<?php echo $product->virtuemart_product_id ?>">

<?php if( $product->product_unit && VmConfig::get('vm_price_show_packaging_pricelabel')) {
echo "<strong>"JText::_('COM_VIRTUEMART_CART_PRICE_PER_UNIT').' ('.$product->product_unit."):</strong>";
}
if(empty($product->prices) and VmConfig::get('askprice',1) and empty($product->images[0]->file_is_downloadable) ){
echo JText::_('COM_VIRTUEMART_PRODUCT_ASKPRICE');
}

//todo add config settings
if( $this->showBasePrice){
echo $this->currency->createPriceDiv('basePrice','COM_VIRTUEMART_PRODUCT_BASEPRICE',$product->prices);
echo $this->currency->createPriceDiv('basePriceVariant','COM_VIRTUEMART_PRODUCT_BASEPRICE_VARIANT',$product->prices);
}
echo $this->currency->createPriceDiv('variantModification','COM_VIRTUEMART_PRODUCT_VARIANT_MOD',$product->prices);

//Comprobamos descuento.

if ($this->currency->createPriceDiv('discountAmount','COM_VIRTUEMART_PRODUCT_DISCOUNT_AMOUNT',$product->prices)) 

{
echo $this->currency->createPriceDiv('basePriceWithTax','COM_VIRTUEMART_PRODUCT_BASEPRICE_WITHTAX',$product->prices); 

}

echo $this->currency->createPriceDiv('discountedPriceWithoutTax','COM_VIRTUEMART_PRODUCT_DISCOUNTED_PRICE',$product->prices);
echo $this->currency->createPriceDiv('salesPriceWithDiscount','COM_VIRTUEMART_PRODUCT_SALESPRICE_WITH_DISCOUNT',$product->prices);
echo $this->currency->createPriceDiv('salesPrice','COM_VIRTUEMART_PRODUCT_SALESPRICE',$product->prices);

echo $this->currency->createPriceDiv('priceWithoutTax','COM_VIRTUEMART_PRODUCT_SALESPRICE_WITHOUT_TAX',$product->prices);

if ($this->currency->createPriceDiv('discountAmount','COM_VIRTUEMART_PRODUCT_DISCOUNT_AMOUNT',$product->prices))

{
echo $this->currency->createDiscountDiv('discountLabel','COM_VIRTUEMART_PRODUCT_DISCOUNT_PER_CENT',$this->currency->formatNumber($product->prices['discountAmount']),$this->currency->formatNumber($product->prices['basePriceWithTax']));
}
echo $this->currency->createPriceDiv('taxAmount','COM_VIRTUEMART_PRODUCT_TAX_AMOUNT',$product->prices); 
?>


</div>
<?php ?>



Open product view on templates/your_template_name/html/com_virtuemart/productdetails/default_product.php


<?php // Output Product Price
if ($this->show_prices and (empty($this->product->images[0]) or $this->product->images[0]->file_is_downloadable==0) ){ ?>

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

if(empty($this->product->prices) and VmConfig::get('askprice',1)  ){ ?>

<a class="ask-a-question bold" href="<?php echo $url ?>" ><?php echo JText::_('COM_VIRTUEMART_PRODUCT_ASKPRICE'?></a>
<?php }

if ($this->showBasePrice) {
echo $this->currency->createPriceDiv 'basePrice''COM_VIRTUEMART_PRODUCT_BASEPRICE'$this->product->prices );
echo $this->currency->createPriceDiv 'basePriceVariant''COM_VIRTUEMART_PRODUCT_BASEPRICE_VARIANT'$this->product->prices );
}

echo $this->currency->createPriceDiv 'variantModification''COM_VIRTUEMART_PRODUCT_VARIANT_MOD'$this->product->prices );

//Comprobamos descuento.
if ($this->currency->createPriceDiv('discountAmount','COM_VIRTUEMART_PRODUCT_DISCOUNT_AMOUNT',$this->product->prices)) 
{
echo $this->currency->createPriceDiv('basePriceWithTax','COM_VIRTUEMART_PRODUCT_BASEPRICE_WITHTAX',$this->product->prices); 

}

echo $this->currency->createPriceDiv 'discountedPriceWithoutTax''COM_VIRTUEMART_PRODUCT_DISCOUNTED_PRICE'$this->product->prices );
echo $this->currency->createPriceDiv 'salesPriceWithDiscount''COM_VIRTUEMART_PRODUCT_SALESPRICE_WITH_DISCOUNT'$this->product->prices );
echo $this->currency->createPriceDiv 'salesPrice''COM_VIRTUEMART_PRODUCT_SALESPRICE'$this->product->prices );
echo $this->currency->createPriceDiv 'priceWithoutTax''COM_VIRTUEMART_PRODUCT_SALESPRICE_WITHOUT_TAX'$this->product->prices );
//Calculamos %
if ($this->currency->createPriceDiv('discountAmount','COM_VIRTUEMART_PRODUCT_DISCOUNT_AMOUNT',$this->product->prices))
{
echo $this->currency->createDiscountDiv('discountLabel','COM_VIRTUEMART_PRODUCT_DISCOUNT_PER_CENT',$this->currency->formatNumber($this->product->prices['discountAmount']),$this->currency->formatNumber($this->product->prices['basePriceWithTax']));
}
echo $this->currency->createPriceDiv 'taxAmount''COM_VIRTUEMART_PRODUCT_TAX_AMOUNT'$this->product->prices ); ?>

</div>
<?php ?>


We´ve finished with views.

Editing language ini file:

If you want use my solution, add COM_VIRTUEMART_PRODUCT_DISCOUNT_PER_CENT on ini language file.

In my case, i´ve decided use images against text. If you want to use images you must write like this:

COM_VIRTUEMART_PRODUCT_DISCOUNT_PER_CENT="<img src='../images/stories/example.png' width='10' height='10' alt='Example Image' />"

If you use "" inside html sentence you get some errors when it is parse. Use simple ' '.

Well. I´m finished.

[attachment cleanup by admin]