How to update options of a configurable product in the Magento Cart

Magento Configurable Option UpdateFor a project I had to figure out how to update options of a configurable product in the Magento Cart.

Considering the code from the famous Inchoo post from 2009 does not work anymore in Magento 1.8+ and I managed to get it working I decided to document the function for my fellow Magento developers out there on the interwebs. The code from the Inchoo post does change a few parts of the quote item but misses a few vital parts like changing the SKU. So on the frontend you might think it has properly changed the product, it will even show up fine in your cart, but once you checkout it will still use the old product! Turns out there’s a cart function called updateItem which can not only change the quantity but also the selected option of configurable.

Hope you’re all happy with this one, it took one of our developers more than 2 weeks to not get it working and I managed it in a day, once again. Mind you, a day is still frustratingly long for me, hence the post because everyone should be able to save that amount of time while working in a project..

The function I wrote to update options of a configurable product in the Magento Cart

To update the flavor of a configurable project we currently use a url like this:
http://somedomain.com/modulename/cart/updateajaxcart?productId=7260&itemId=3004&productQty=1&supperAttributeId=175&childId=237

Some explanation of the various variables you’re supposed to pass to the function:

  • $superAttributeId is the id of the attribute, in our case flavor which has id 175.
  • $childId is the id of the selected option of the attribute
  • $itemId is the id of the item in the cart
  • $productId is the id of the actual product, in this case the configurable
  • $refresh: if set will force the current page to refresh after updating the cart (handy for cart page since you want to not only refresh the ajax cart but also the whole cart page)

I cleaned up the code while writing the post so an error might have slipped in during the process, if you find anything wrong with it please don’t hesitate to comment and I will fix the code

5 comments

  1. can you send me full source code i will appreciate….is it working for multiple options

  2. Haha sorry no, this is the trickiest part of it all and the only part of the code I wrote. The frontend part was written by a colleague..

  3. I am using this exact technique for updating a Custom Option

    However, at this part:
    $item = $cart->updateItem($itemId, new Varien_Object($params));

    the system boots me back to the Cart view with the following error:

    “Please specify the product required option(s).”

    the $params object I am trying to send in looks like this:

    array(4) {
    [“id”]=> string(5) “75555”
    [“qty”]=> int(1)
    [“product”]=> string(4) “1420”
    [“super_attribute”]=> array(1) {
    [177]=> string(3) “670” }
    }

    can you see any reason why the validation it would fail?

  4. @Sean are you sure you’re updating a configurable product and not a product using only options?

Leave a Reply