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

VM [2.0.6] Customfield trigger bug

Started by iMedved, May 30, 2012, 21:39:54 PM

Previous topic - Next topic

iMedved

First product in cart with custom fields dont trigger plgVmOnViewCartModule
Part of cart object

    ["1::0"]=>
    object(stdClass)#231 (44) {}
    ["1::1"]=>
    object(stdClass)#230 (44) {}


Problem arises because method public function parseModifier($name) class calculationHelper uses function empty instead isset.

//name = 1::0
public function parseModifier($name) {

$variants = array();

if ($index = strpos($name, '::')) {

$virtuemart_product_id = substr($name, 0, $index);
                                // $virtuemart_product_id = 1
$allItems = substr($name, $index + 2);
                                // $allItems = 0
$items = explode(';', $allItems);
// $items array('0' => 0)
foreach ($items as $item) {
                                        // $item = 0
                                        // !empty(0) = false
if (!empty($item)) {
$index2 = strpos($item, ':');
$variant = substr($item, 0, $index2);
$selected = substr($item, $index2 + 1);
$variants[$variant] = $selected;
}
}
}


Returns FALSE if var has a non-empty and non-zero value.
http://php.net/manual/en/function.empty.php

Milbo

hmm, when it is empty, then it should be false, that is correct. it means there is no variant, I think there is an other error.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

iMedved

#2
Quote from: Milbo on May 30, 2012, 22:52:35 PM
hmm, when it is empty, then it should be false, that is correct. it means there is no variant, I think there is an other error.
Sorry im not good in english. Try to explain.

We adding to cart product with custom plugin field.
Got in cart object
["products"]=>
  array(1) {
    ["1::0"]=>object(stdClass)#237 (44) {


Adding again:
Got in cart object
["products"]=>
  array(2) {
    ["1::0"]=>object(stdClass)#237 (44) {}
    ["1::1"]=>object(stdClass)#236 (44) {}


Now going to cart ViewJS and see that second product call plugin plgVmOnViewCartModule - got data in product_attributes, first not.

Ok.
/public/components/com_virtuemart/helpers/cart.php->prepareAjaxData()
foreach ($this->products as $priceKey=>$product){
....
if (!is_numeric($priceKey)) {
if(!class_exists('VirtueMartModelCustomfields'))require(JPATH_VM_ADMINISTRATOR.DS.'models'.DS.'customfields.php');
//  custom product fields display for cart
$this->data->products[$i]['product_attributes'] = VirtueMartModelCustomfields::CustomsFieldCartModDisplay($priceKey,$product);

}

/public/administrator/components/com_virtuemart/models/customfields.php

public function CustomsFieldCartModDisplay($priceKey,$product) {
.....
if ($variantmods) {
.....
$dispatcher->trigger('plgVmOnViewCartModule',array($product, $row,&$html));

In second iteration we got true and trigger method, in first not.

Looking for  $variantmods
$variantmods = $calculator->parseModifier($priceKey);


/public/administrator/components/com_virtuemart/helpers/calculationh.php

public function parseModifier($name) {

$variants = array();
if ($index = strpos($name, '::')) {
$virtuemart_product_id = substr($name, 0, $index);
$allItems = substr($name, $index + 2);
$items = explode(';', $allItems);

foreach ($items as $item) {
if (!empty($item)) {
$index2 = strpos($item, ':');
$variant = substr($item, 0, $index2);
$selected = substr($item, $index2 + 1);
$variants[$variant] = $selected;
}
}
}
return $variants;
}


What this function should do.
1. if it got 1::, it will return empty array - array(0) { }. Ok.
2. If it got 1::1, it will return non empty array - array(1) { [""]=> bool(false) } - so if ($variantmods) - true and trigger plugin method
3. If it got 1::0, it have to return non empty array, but it doesnt. Cause in first iteration of foreach ($items as $item), $item - will be zero. It set, but zero. For empty function it will be true.

Solution:
1. Use isset. Case
var $i=0;
if(isset($i)) = true
if(!empty($i)) = false
2. then vm adding to cart product with modifers - avoid zero :)

iMedved

Quote from: lipes on May 30, 2012, 23:29:17 PM
humm.. could be this the same reason for problem that i am experiencing?
Do u use custom field plugin?

iMedved


Milbo

#5
Quote from: lipes on May 30, 2012, 23:29:17 PM
But when add the product to the cart.. the product give us a different value from what we've choose...
http://outros.net23.net/index.php?option=com_virtuemart&view=productdetails&virtuemart_product_id=8&virtuemart_category_id=2

I think the price for the color is always added.

iMedved, so you say it does not work correctly, when the selection is a 0? And that the tests if there is a variant are already done, so it is strange? Sounds reasonable. So what are your experience changing it to isset? Did you got any other trouble?
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

lipes

#6
Yep!
Always add the Size '150'  (that have the price 77.55€), even if you choose no size (zero/ empty value);  color always got the Yellow price ( 0,97 € );  and add a showel price "10,34 €"

that give us the final result in cart: 313,30 €
The Text of Custom Fields is working correct in the cart... but the price Custom Field it isnt ...


I've tested again and it's a problem related with 2.0.7 (a,b,c,d,) sorry! >> http://forum.virtuemart.net/index.php?topic=103126.msg343658#msg343658
VM V. online: J2.5.14 | VM 2.0.20a | SQL 5.1.70 | PHP 5.3.25

iMedved