News:

Support the VirtueMart project and become a member

Main Menu

Product attribute - Percentage intead fixed value?

Started by neutrino86, February 17, 2012, 20:10:52 PM

Previous topic - Next topic

neutrino86

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,

francescomac82


valir

I have the same problem!

How is it going?

What code is needed to change?

eliseooo


mmbax

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!!!

vmlab

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]
If my hacks or tweaks have helped you in anyway, please consider making a donation to vmlabz@gmail.com via PayPal

You can check out my hacks @ http://vmlabz.blogspot.in/

VM Attribute Manager https://docs.google.com/document/d/1aI2P3WvdVqzp7cexUHFcOFvDp0Hy7FuDyWrAeSzuhwU/edit?authkey=CKTO8vsD&hl=en_US


VM Price Manager https://docs.google.com/document/d/1ybtPs2Lzve_mxP5rypo4i01uZx6ZEoxK3hcC-8Sc6Ic/edit?hl=en_US

francescomac82

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.

vmlab

#7
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
If my hacks or tweaks have helped you in anyway, please consider making a donation to vmlabz@gmail.com via PayPal

You can check out my hacks @ http://vmlabz.blogspot.in/

VM Attribute Manager https://docs.google.com/document/d/1aI2P3WvdVqzp7cexUHFcOFvDp0Hy7FuDyWrAeSzuhwU/edit?authkey=CKTO8vsD&hl=en_US


VM Price Manager https://docs.google.com/document/d/1ybtPs2Lzve_mxP5rypo4i01uZx6ZEoxK3hcC-8Sc6Ic/edit?hl=en_US

demo-imp

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