VirtueMart Forum

VirtueMart 2 + 3 + 4 => Administration & Configuration => Topic started by: neutrino86 on February 17, 2012, 20:10:52 PM

Title: Product attribute - Percentage intead fixed value?
Post by: neutrino86 on February 17, 2012, 20:10:52 PM
Hi,

I would add a percentage instead of a fixed value when that one selects an attribute, it is possible?
My product has a price variable so the attribute can not be a fixed value.

For exemple :

red +10%
blue +15%
green +20%

Thanks,
Title: Re: Product attribute - Percentage intead fixed value?
Post by: francescomac82 on May 12, 2012, 12:03:52 PM
I have the same problem!
Any solution?

Thanks
Title: Re: Product attribute - Percentage intead fixed value?
Post by: valir on June 07, 2012, 01:12:30 AM
I have the same problem!

How is it going?

What code is needed to change?
Title: Re: Product attribute - Percentage intead fixed value?
Post by: eliseooo on September 03, 2012, 13:47:17 PM
hi all
someone found the solution ?
Title: Re: Product attribute - Percentage intead fixed value?
Post by: mmbax on October 19, 2012, 07:43:03 AM
I have also been trying to use * and / as well as + and -.

there are solutions for VM1 but nobody seems to know anything about VM2!

CAN ANYBODY PLEASE HELP!!!
Title: Re: Product attribute - Percentage intead fixed value?
Post by: vmlab on October 24, 2012, 14:17:29 PM
Hi

To achieve this functionality you have to modify two files

1. calculationh.php
   located at mysite\administrator\components\com_virtuemart\helpers
   
   and
   
2. customfields.php
    located at mysite\administrator\components\com_virtuemart\models
   
   
In calculationh.php, in the function calculateModificators

Replace the lines of code

if (!empty($productCustomsPrice->custom_price)) {
//TODO adding % and more We should use here $this->interpreteMathOp

$modificatorSum = $modificatorSum + $productCustomsPrice->custom_price;
}


with

if (!empty($productCustomsPrice->custom_price)) {
//TODO adding % and more We should use here $this->interpreteMathOp
//@vmlab
//$costPrice = isset($product->product_price)? $product->product_price:0;
if( substr($productCustomsPrice->custom_price,0,1) == '*' || substr($productCustomsPrice->custom_price,0,1) == '/'  || substr($productCustomsPrice->custom_price,0,2) == '+%'  || substr($productCustomsPrice->custom_price,0,2) == '-%' )
{
$price_length = strlen($productCustomsPrice->custom_price);
$price_float = -1;
$modification_sign = '';
$final_price = 0.0;
if(substr($productCustomsPrice->custom_price,0,1) == '*' )
{
$price_float = (float)substr($productCustomsPrice->custom_price,1,$price_length);
if($price_float > 0)
{
$final_price = $product->product_price * $price_float;
$modificatorSum = $final_price - $product->product_price;
}
//$modification_sign = substr($productCustomsPrice->custom_price,0,1);
}
else if(substr($productCustomsPrice->custom_price,0,1) == '/' )
{
$price_float = (float)substr($productCustomsPrice->custom_price,1,$price_length);
if($price_float > 0)
{
$final_price =  $product->product_price / $price_float;
$modificatorSum = $final_price - $product->product_price;
}
//$modification_sign = substr($productCustomsPrice->custom_price,0,1);
}
else if(substr($productCustomsPrice->custom_price,0,2) == '+%' )
{
$price_float = (float)substr($productCustomsPrice->custom_price,2,$price_length);
if($price_float > 0)
{
$modificatorSum  =  ($product->product_price * $price_float)/100;
}
}
else if(substr($productCustomsPrice->custom_price,0,2) == '-%' )
{
$price_float = (float)substr($productCustomsPrice->custom_price,2,$price_length);
if($price_float > 0)
{
$modificatorSum  =  -1 * (($product->product_price * $price_float)/100);
}
}


}
$modificatorSum = $modificatorSum + $productCustomsPrice->custom_price;
}



In customfields.php, in the function getProductCustomsFieldCart

Replace the lines of code

foreach ($group->options as $productCustom) {
if ((float)$productCustom->custom_price)
{
$price = strip_tags ($currency->priceDisplay ($calculator->calculateCustomPriceWithTax ($productCustom->custom_price)));
}
else
{
$price = ($productCustom->custom_price === '') ? '' : $free;
}
$productCustom->text = $productCustom->custom_value . ' ' . $price;

}


with

foreach ($group->options as $productCustom) {
if ((float)$productCustom->custom_price)
{
$price = strip_tags ($currency->priceDisplay ($calculator->calculateCustomPriceWithTax ($productCustom->custom_price)));
}
//@vmlab modification
else if( substr($productCustom->custom_price,0,1) == '*' || substr($productCustom->custom_price,0,1) == '/'  || substr($productCustom->custom_price,0,2) == '+%'  || substr($productCustom->custom_price,0,2) == '-%' )
{
$price_length = strlen($productCustom->custom_price);
$price_float = -1;
$modification_sign = '';
if(substr($productCustom->custom_price,0,1) == '*' || substr($productCustom->custom_price,0,1) == '/')
{
$price_float = (float)substr($productCustom->custom_price,1,$price_length);
$modification_sign = substr($productCustom->custom_price,0,1);
}
else if(substr($productCustom->custom_price,0,2) == '+%'  || substr($productCustom->custom_price,0,2) == '-%' )
{
$price_float = (float)substr($productCustom->custom_price,2,$price_length);
$modification_sign = substr($productCustom->custom_price,0,2);
}

if($price_float != -1)
{
$price = $modification_sign.' '.strip_tags ($currency->priceDisplay ($calculator->calculateCustomPriceWithTax ($price_float)));
}
}
else
{
$price = ($productCustom->custom_price === '') ? '' : $free;
}
$productCustom->text = $productCustom->custom_value . ' ' . $price;

}



I am attaching the modified files. Please do experiment with it after making a backup of your site.

[attachment cleanup by admin]
Title: Re: Product attribute - Percentage intead fixed value?
Post by: francescomac82 on October 24, 2012, 14:40:24 PM
It doesn't work on my site!
I noticed that my customfields.php is different from yours, I'm using virtuemart 2.0.12c

My file have those lines commented this way:

foreach ($group->options as $productCustom) {
/* if ((float)$productCustom->custom_price) {
$price = $currency->priceDisplay ($calculator->calculateCustomPriceWithTax ($productCustom->custom_price));
}
else {
$price = ($productCustom->custom_price === '') ? '' : $free;
}*/
$productCustom->field_type = $group->field_type;
$productCustom->is_cart = 1;
$group->display .= $this->displayProductCustomfieldFE ($product, $productCustom, $row);
$checked = '';
}




Quote from: vmlab on October 24, 2012, 14:17:29 PM
Hi

To achieve this functionality you have to modify two files

1. calculationh.php
   located at mysite\administrator\components\com_virtuemart\helpers
   
   and
   
2. customfields.php
    located at mysite\administrator\components\com_virtuemart\models
   
   
In calculationh.php, in the function calculateModificators

Replace the lines of code

if (!empty($productCustomsPrice->custom_price)) {
//TODO adding % and more We should use here $this->interpreteMathOp

$modificatorSum = $modificatorSum + $productCustomsPrice->custom_price;
}


with

if (!empty($productCustomsPrice->custom_price)) {
//TODO adding % and more We should use here $this->interpreteMathOp
//@vmlab
//$costPrice = isset($product->product_price)? $product->product_price:0;
if( substr($productCustomsPrice->custom_price,0,1) == '*' || substr($productCustomsPrice->custom_price,0,1) == '/'  || substr($productCustomsPrice->custom_price,0,2) == '+%'  || substr($productCustomsPrice->custom_price,0,2) == '-%' )
{
$price_length = strlen($productCustomsPrice->custom_price);
$price_float = -1;
$modification_sign = '';
$final_price = 0.0;
if(substr($productCustomsPrice->custom_price,0,1) == '*' )
{
$price_float = (float)substr($productCustomsPrice->custom_price,1,$price_length);
if($price_float > 0)
{
$final_price = $product->product_price * $price_float;
$modificatorSum = $final_price - $product->product_price;
}
//$modification_sign = substr($productCustomsPrice->custom_price,0,1);
}
else if(substr($productCustomsPrice->custom_price,0,1) == '/' )
{
$price_float = (float)substr($productCustomsPrice->custom_price,1,$price_length);
if($price_float > 0)
{
$final_price =  $product->product_price / $price_float;
$modificatorSum = $final_price - $product->product_price;
}
//$modification_sign = substr($productCustomsPrice->custom_price,0,1);
}
else if(substr($productCustomsPrice->custom_price,0,2) == '+%' )
{
$price_float = (float)substr($productCustomsPrice->custom_price,2,$price_length);
if($price_float > 0)
{
$modificatorSum  =  ($product->product_price * $price_float)/100;
}
}
else if(substr($productCustomsPrice->custom_price,0,2) == '-%' )
{
$price_float = (float)substr($productCustomsPrice->custom_price,2,$price_length);
if($price_float > 0)
{
$modificatorSum  =  -1 * (($product->product_price * $price_float)/100);
}
}


}
$modificatorSum = $modificatorSum + $productCustomsPrice->custom_price;
}



In customfields.php, in the function getProductCustomsFieldCart

Replace the lines of code

foreach ($group->options as $productCustom) {
if ((float)$productCustom->custom_price)
{
$price = strip_tags ($currency->priceDisplay ($calculator->calculateCustomPriceWithTax ($productCustom->custom_price)));
}
else
{
$price = ($productCustom->custom_price === '') ? '' : $free;
}
$productCustom->text = $productCustom->custom_value . ' ' . $price;

}


with

foreach ($group->options as $productCustom) {
if ((float)$productCustom->custom_price)
{
$price = strip_tags ($currency->priceDisplay ($calculator->calculateCustomPriceWithTax ($productCustom->custom_price)));
}
//@vmlab modification
else if( substr($productCustom->custom_price,0,1) == '*' || substr($productCustom->custom_price,0,1) == '/'  || substr($productCustom->custom_price,0,2) == '+%'  || substr($productCustom->custom_price,0,2) == '-%' )
{
$price_length = strlen($productCustom->custom_price);
$price_float = -1;
$modification_sign = '';
if(substr($productCustom->custom_price,0,1) == '*' || substr($productCustom->custom_price,0,1) == '/')
{
$price_float = (float)substr($productCustom->custom_price,1,$price_length);
$modification_sign = substr($productCustom->custom_price,0,1);
}
else if(substr($productCustom->custom_price,0,2) == '+%'  || substr($productCustom->custom_price,0,2) == '-%' )
{
$price_float = (float)substr($productCustom->custom_price,2,$price_length);
$modification_sign = substr($productCustom->custom_price,0,2);
}

if($price_float != -1)
{
$price = $modification_sign.' '.strip_tags ($currency->priceDisplay ($calculator->calculateCustomPriceWithTax ($price_float)));
}
}
else
{
$price = ($productCustom->custom_price === '') ? '' : $free;
}
$productCustom->text = $productCustom->custom_value . ' ' . $price;

}



I am attaching the modified files. Please do experiment with it after making a backup of your site.
Title: Re: Product attribute - Percentage intead fixed value?
Post by: vmlab on October 24, 2012, 15:03:03 PM
Virtuemart 2.0.12c seems to have the DB altered.

The field custom_price has been modified to be of the type decimal where as it was of the type char in VM 2.0.8e.

So if anyone wants to get this hack working for the latest version of VM2 please PM me with a quote
Title: Re: Product attribute - Percentage intead fixed value?
Post by: demo-imp on December 14, 2012, 10:51:45 AM
Hello,

Using VM2.0.7, this hack works well for me if i don't use any other custom field.

My problem is:
I created a pluggin, based on custom input-text, which calculate price with the value of this input text.

if i allocate both custom input text and your hack in another custom field, there is a calculation conflict.


In your hack, you calculate a modificatorsum  = product_price * custom price in %
In my pluggin, i calculate product_price = product_price * custom input text value

Please could you say which variable we have to change in order to mix both solutions?

modificatorsum?
product_price?
final_price?
custom_price?

Sorry for my english or if i'm not clear, not very easy to explain..

thanks