Magento – Update cart quantity for a grouped product – grouped products not properly supported in magento core code

cartgrouped-productsmagento-1

I'm going mad on this one

Scenario:
my simple products all hidden.
what the visitor sees are grouped products with the simple products associated.

I can add to the cart programatically:

$params['product']=$productid;
$params['super_group'] = array( $simpleid=> $qty );
$cart = Mage::getModel('checkout/cart'); //Get cart object      
$cart->init();
$cart->addProduct($productid, $params);
$cart->save(); 

This will create the product in the cart properly, which means that the resulting product links to the product page.

I can use the same code to increase the quantity too.

I can remove from the cart properly – finding the cart row id for the simple product from $session->getQuote()->getAllVisibleItems()

$cartHelper = Mage::helper('checkout/cart');
$cartHelper->getCart()->removeItem($simpleproductcartid)->save();

Can I change quantity, and especially decrease quantity? NO LUCK

In theory it should be as simple as:

$cartHelper->getCart()->updateItem($somecartrowid, $params)->save();

This works for bundles and simple products, but in my grouped product scenario, it's a mess.

I end up having:

  • the original row for the product, which is properly aware of the grouped nature and has a link, with the original amount
  • a new additional row for the simple product, with the new quantity
  • when looking in code at getAllVisibleItems() only 1 row is visible, with the new quantity

BEFORE

before changing quantity

AFTER

after changing quantity

I have tried passing just quantity, as well as passing a super_group, seems to make no difference.

Logically the way I have built things internally there should be a grouped product quantity of 1 with then the super_group quantity for the individual simple product which changes as I add/change the quantity. At least I assume so?

I have also tried setting my code remove the product and add the new quantity but then the cart appears empty at the end.

I'm not sure if I am doing the change wrong or if I am doing the original adding wrong as well, but this has me stumped.

Hoping someone has tackled that already else I will have to go dig in the code, and I am still quite lost in the Magento source.

To explain some more, in case someone can see:

In the cart I have grouped product 635, associated product 122, quantity 3

{"name":"Amigne 2012", "type":"grouped", "parent_id":"635", "entity_id":"567", "qty":3}

if i have a look at the cart/quote, via $session->getQuote()->getAllVisibleItems() I see that the cart id is 989 and it is associated to the simple product ID

"567":{"cartid":"1000","qty":3},

But if I call

$cart->updateItem('1000',1)

The cart suddenly acquires a row with the simple product – and yes, a different cartid (I told it to update '1000' and i get '1002')

{"name":"Amigne 2012", "type":"simple", "parent_id":"", "entity_id":"567", "qty":1}

"567":{"cartid":"1002","qty":1}

note that in the data dump the old row with the grouped product doesn't show, but on the cart page it still shows, right above the new one with the simple product (see images)

I can see that the updateItem Cart method goes deeper to the Quote Model file

public function updateItem($itemId, $buyRequest, $params = null)
     * @param int $itemId
     * @param Varien_Object $buyRequest
     * @param null|array|Varien_Object $params
     * @return Mage_Sales_Model_Quote_Item

and in turn my mysterious product switch must happen when this calls the Catalog Product Helper

public function addParamsToBuyRequest($buyRequest, $params)
     * $params holds parameters of what operation must be performed:
     * - 'current_config', Varien_Object or array - current buyRequest that configures product in this item,
     *   used to restore currently attached files
     * - 'files_prefix': string[a-z0-9_] - prefix that was added at frontend to names of file inputs,
     *   so they won't intersect with other submitted options
     *
     * @param Varien_Object|array $buyRequest
     * @param Varien_Object|array $params
     * @return Varien_Object

I can see that this routine does some add/remove of products if it thinks options have changed and I can only imaging this is the core of my bug…

…but I cannot for the life of me spot what I need to pass in either $buyRequest or $params to keep my Grouped Product structure. It's not the same super_group structure that works on adding to cart.

Best Answer

This is not a great answer as it does not fix the original bug, just takes a totally different route. I'd still like to understand what went wrong with my original logic (why does it not work for grouped)

The only way i have found which doesn't create odd structural messes is to totally "emulate" the submitting of the shopping cart form.

Digging deep in the structure of Quotes and Products did not allow me to trace a valid syntax for passing parameters to updateItem in the case of a grouped product, so I eventually went back to have a look at updateItems instead

1) build the params to exactly match the structure of the form eg $cartdata

$cartdata[988][qty]=100
$cartdata[999][qty]=1
$cartdata[1001][qty]=1

2) call $cart->updateItems($cartdata);

Now this does not allow me to do any additional error checking, so I will probably need to copy updateItems and modify it for my needs. (otherwise how will I tell users the stock's not there?)

This doesn't feel very upgrade proof but at least I can change quantities without it all breaking

I note that updateItems does not call updateItem but simply calls $item->setQty($qty); - from Sales Quote Item

Related Topic