Updating options of configurable product that is already in the cart

Featured Image

Let’s say we have configurable Magento product in shopping cart and we want to update its
options without deleting product from cart and adding it again.

That’s quite easy to achieve with following steps, however I can’t write complete tutorial here so
you should be a little more familiar with Magento in order to make it work.

First open file app/design/frontend/YOUR_INTERFACE/YOUR_THEME/template/checkout/cart/item/default.phtml
Find line with this code:

< ?php if ($_options = $this->getOptionList()):?>

[*]
Bellow that line put this code:

< ?php
if($this->getProduct()->isConfigurable()){
$_product = Mage::getModel('catalog/product')->load($this->getProduct()->getId());
Mage::getBlockSingleton('catalog/product_view_type_configurable')->unsetData();
$_configurable = Mage::getBlockSingleton('catalog/product_view_type_configurable')->setData('product', $_product);
$_cdata = json_decode($_configurable->getJsonConfig());
$_current = array();
foreach((array)$this->getOptionList() as $_option) {
$_current[$_option['label']]=$_option['value'];
}
foreach($_cdata->attributes as $attribute) {
?>
<strong>< ?php echo $attribute->label; ?></strong>
<select style="width:150px;" name="cart[<?php echo $_item->getId() ?>][option][< ?php echo $attribute->id ?>]">
< ?php
foreach($attribute->options as $option) {
?>
<option <?php echo ($_current[$attribute->label]==$option->label) ? ' selected' : '' ?> value="< ?php echo $option->id ?>">< ?php echo $option->label ?> < ?php echo $this->helper('checkout')->formatPrice($option->price+$_item->getProduct()->getPrice()) ?></option>
< ?php
}
?>
</select>
< ?php
}
} else {
// THIS IS PLACE WHERE EXISTING CODE from [*] goes
}
?>

Now you are done with template and you can style it as you wish as soon as you do few additional steps. 🙂

Next thing you should do is:

Inside app/code/local/ create directory YOUR_FIRM/MODULE_NAME/ and there you make directories “Model” and “etc”.

Create these files there:
config.xml inside “etc” that contains following:

< ?xml version="1.0"?>
<config>
<frontend>
<events>
<checkout_cart_update_items_before>
<observers>
<your_firm_module_name_event>
<type>singleton</type>
<class>YOUR_FIRM_MODULE_NAME_Model_Card</class>
<method>update</method>
</your_firm_module_name_event>
</observers>
</checkout_cart_update_items_before>
</events>
</frontend>
</config>

and

Card.php inside “Model” that contains following:

< ?php
 
class YOUR_FIRM_MODULE_NAME_Model_Card
{
public function update($e)
{
$_this = $e->cart;
$data = $e->info;
 
foreach ($data as $itemId => $itemInfo) {
$item = $_this->getQuote()->getItemById($itemId);
 
if (!$item) continue;
if (!isset($itemInfo['option']) or empty($itemInfo['option'])) continue;
 
foreach ($item->getOptions() as $option){
 
if($option->getCode()=='info_buyRequest'){
 
$unserialized = unserialize($option->getValue());
$unserialized['super_attribute'] = $itemInfo['option'];
$option->setValue(serialize($itemInfo['option']));
 
}elseif ($option->getCode()=='attributes'){
$option->setValue(serialize($itemInfo['option']));
}
 
}
$item->save();
}
}
 
}
?>

Ok, now just create file YOUR_FIRM_MODULE_NAME.xml inside app/etc/
and put this inside:

< ?xml version="1.0"?>
<config>
<modules>
<your_firm_module_name>
<codepool>local</codepool>
<active>true</active>
</your_firm_module_name>
</modules>
</config>

That’s about it 🙂 Enjoy!

P.S. Yes, I know… before clicking on update cart, we should have some JavaScript price changer, but this is all I have at this moment since I needed this on card that is autosubmited on dropdown change. 🙂


About Tomas Novoselic

Team Leader/Backend Developer

At Inchoo, Tomas is a Team Leader and Certified Magento Developer who handles Magento modifications at any level. He also works closely with clients on Magento projects of any size and difficulty.

Read more posts by Tomas / Visit Tomas's profile

48 comments

  1. Hi , I have tried it and it working fine for me , But i need update options of bundle product from cart page. Can you provide me some hint for bundle type products.

    I hope you will give me some hint for the same

    Thanks for awesome post
    OM

  2. Nothing working after updating cart getting error
    * Please specify the product’s option(s).

    then Checkout button disappeared.

    Please help

  3. @rakesh :
    Hi, you can use this code below to fix:
    public function update($e)
    {
    $_this = $e->cart;
    $data = $e->info;
    foreach ($data as $itemId => $itemInfo) {
    $item = $_this->getQuote()->getItemById($itemId);

    if (!$item) continue;
    if (!isset($itemInfo[‘option’]) or empty($itemInfo[‘option’])) continue;
    $options = $item->getOptions();

    foreach ($options as $option){
    if($option->getCode()==’info_buyRequest’){
    $unserialized = unserialize($option->getValue());
    $unserialized[‘super_attribute’] = $itemInfo[‘option’];
    $option->setValue(serialize($unserialized));

    }elseif ($option->getCode()==’attributes’)
    {
    $option->setValue(serialize($itemInfo[‘option’]));
    }
    }

    $item->setOptions($options)->save();
    Mage::getSingleton(‘checkout/cart’)->save();
    }
    }

    1. This line was the clue:
      Mage::getSingleton(‘checkout/cart’)->save();
      Thank you, it works for me with magento 1.7

  4. This code is not worked with Mangento 1.7.so anyone can advice me to where i will go for managing this code…Thanks an advance..

  5. Hello Tomas,

    I have used your code on my cart page. I does change the attributes on cart page. But when I place an order it is showing me the different product than the option I updated in cart.
    Let me explain.
    If I have a product with color options having value Pink and Blue, now I add each of the product in cart. And when I change the color of blue product to pink it should increase the quantity of the pink product to 2 but it remains 1 and the blue products remains as a different product with pink option selected.
    So, now i have two products in my cart with the same options and each of their quantity is 1.
    Now if I place order and see the SKU in my order detail page, It displays me one product of blue and the other of pink.
    In short, it is not actually changing product options or am I missing something?
    Need your help.

  6. Hi, About update item cart option
    You can try using this code :

    public function update($e)
    {
    $_this = $e->cart;
    $data = $e->info;
    foreach ($data as $itemId => $itemInfo) {
    $item = $_this->getQuote()->getItemById($itemId);
    //$item = Mage::getSingleton(‘checkout/session’)->getQuote()->getItemById($itemId);
    if (!$item) continue;
    if (!isset($itemInfo[‘option’]) or empty($itemInfo[‘option’])) continue;
    $options = $item->getOptions();

    foreach ($options as $option){
    if($option->getCode()==’info_buyRequest’){
    $unserialized = unserialize($option->getValue());
    $unserialized[‘super_attribute’] = $itemInfo[‘option’];
    $option->setValue(serialize($unserialized));

    }elseif ($option->getCode()==’attributes’)
    {
    $option->setValue(serialize($itemInfo[‘option’]));
    }
    }
    //echo get_class($item);exit();
    $item->setOptions($options)->save();
    Mage::getSingleton(‘checkout/cart’)->save();
    }
    }

  7. Thank for this code,

    I have followed your instruction and added all necessary code but can you help me in updating it, I mean how can I update or call update function on any change.

    I have tried calling using ajax but it doesn’t work.

    Can you please help me in this.
    Thanks.

  8. hi
    I have follow the steps which you have written here and i get success up to display drop down with configure options but i can’t update it?

  9. Options are updated but not the simple product (id and sku) that has the options in the quote.

    Regards,
    Piet

  10. Hello Tomas and bodiandras,

    your solution does not work for me, because it does not change the simple product which is associated with the configurable product, and which depends on the attribute.
    Do you had the same problem?
    I’m using Magento CE 1.4.1.1 😉

    regards,
    michael

  11. I am not able to update my cart option by adding these code in observer.php.here is the update method.
    public function update($e)
    {

    $_this = $e->cart;
    $data = $e->info;
    foreach ($data as $itemId => $itemInfo) {

    $item = $_this->getQuote()->getItemById($itemId);

    if (!$item)
    continue;
    if (!isset($itemInfo[‘option’]) or empty($itemInfo[‘option’]))
    continue;
    $options = $item->getOptions();
    foreach ($options as $option ) {
    $unserialized = unserialize($option->getValue());
    $unserialized[‘super_attribute’] = $itemInfo[‘option’];
    $option->setValue(serialize($unserialized));

    }
    $item->setProductOptions($options);
    $item->save();

    }

    please suggest what is the problem

    Thank you

  12. Hello Arham,
    I pass the options i want to change into to my ajax.php using xml.
    Here is the code for the ajax.php

    $xml=simplexml_load_string($_POST[‘xml’]);
    foreach ($xml->Item as $item)
    {
    $Id=(string)$item->Id; $ColorId=(string)$item->ColorId; $SizeId=(string)$item->SizeId; $Kbnr=(string)$item->Kbnr; $VariationId=(string)$item->VariationId; $Quantity=(string)$item->Quantity;
    }
    if($ColorId!=”) { $itemInfo[‘option’][’80’]=$ColorId; }
    if($SizeId!=”) { $itemInfo[‘option’][‘124’]=$SizeId; }
    if($VariationId!=”){ $itemInfo[‘option’][‘127’]=$VariationId; }

    $Checkout = Mage::getSingleton(‘checkout/session’);
    $Cart = Mage::helper(‘checkout/cart’)->getCart();

    $item=$Checkout->getQuote()->getItemById($Id);

    foreach ($item->getOptions() as $option)
    {
    if($option->getCode()==’info_buyRequest’)
    {
    $unserialized = unserialize($option->getValue());
    //print_r($unserialized);
    $unserialized[‘super_attribute’] = $itemInfo[‘option’];
    $unserialized[‘kbnr’]=$Kbnr;
    $unserialized[‘qty’]=$Quantity;

    $option->setValue(serialize($unserialized));
    /*
    $unserialized = unserialize($option->getValue());
    print_r($unserialized);*/

    }

    elseif ($option->getCode()==’attributes’)
    {
    $option->setValue(serialize($itemInfo[‘option’]));
    }
    }

    $options[‘info_buyRequest’][‘kbnr’]=$Kbnr;
    $item->setProductOptions($options);
    $item->save();

    $quote =$Cart->getQuote();
    foreach ($quote->getItemsCollection() as $item)
    {
    if ($item->getId()==$Id) { $item->setQty($Quantity); echo $item->getQty(); }
    }
    $Cart->save();
    $ResponseXml=”.$Id.’ ;color=’.$ColorId.’; size=’.$SizeId.’;-‘.$msg.”;
    echo $ResponseXml;

  13. hello bodiandras!

    can you tell me how you make it work using ajax plz update your fix here

    thanks….

  14. Thanks for the code but the updates option is not working .if u give the full source.

    1. Updating options of configurable product that is already in the cart not work for me of magento version 1.9.0.1-updates option is not working

  15. Hello Tomas,
    Thanks for code.

    But one options work correctly. How to get the one more associate products option.

  16. Got it !

    $model = Mage::getModel(‘catalog/product’); //getting product model
    $productid = 31;
    $_product = $model->load($productid); //getting product object for particular product id
    foreach ($_product->getOptions() as $_options) {
    echo “TYPE: ” . $_options->getType() . “”;
    echo “TITLE: ” . $_options->getTitle() . “”;
    echo “”;
    $_values = $_options->getValues();
    foreach ($_values as $_options_value) {
    print_r($_options_value->getData());
    }
    $i++;
    echo “”;
    }

  17. Hello!

    Thanks for the code, i managed to make it work in my AJAX modify attribute of product in cart code.

    But i think there is a logical error, i had to correct it for it to work:

    where
    $unserialized = unserialize($option->getValue());
    $unserialized[‘super_attribute’] = $itemInfo[‘option’];
    $option->setValue(serialize($itemInfo[‘option’]));

    change last line to
    $option->setValue(serialize($unserialized));

    Also before
    $item->save();
    i had to use
    $item->setProductOptions($options);

    Best Regards
    Bodi Andras

  18. I am trying to load image for the bundled items, can somebody help me with this? I want to load this on the default.phtml page of the shopping cart.
    I am using Cart2Quote module.
    However I am able to get the thumbnail url using this code in app/code/core/Mage/Bundle/Helper/Catalog/Product/Configuration.php, but the image itself is not getting loaded.
    $option[‘value’][] = $qty . ‘ x ‘ . $this->escapeHtml($bundleSelection->getName()). $bundleSelection->getThumbnailUrl(28,28)
    . ‘ ‘ . Mage::helper(‘core’)->currency($this->getSelectionFinalPrice($item, $bundleSelection));

  19. Hello Tomas,
    Thanks for code.

    But i have same problem as Vas.

    I have follow the steps which you have written here and i get success up to display drop down with configure options but i can’t update it?? also dont get any error..

    also your link “http://www.magentocommerce.com/extension/2042/edit-configuration-product-in-cart” does not work.

    Thanks once again.

  20. hi! thanks for this code. i was able to make it work just by editing the default.phtml file. do you think i’ll run into problems if i don’t do the next steps (adding and editing the xml files)?

  21. Hi,
    thx for this great stuff.
    We have got the same problem like vas. We include everything step by step. We are able to see the dropdown button, but you are not able to change the option of product.
    Any ideas?
    THx for help.
    Kind regards from germany

  22. Thanks for your response Tomas. Here It’s the complete code: http://pastebin.com/f5bd1630

    I’m having the same problem as Vas – The drop downs show fine but there’s no update after I press ‘update cart’.

    thanks for this!

  23. Hi,

    I have follow the steps which you have written here and i get success up to display drop down with configure options but i can’t update it?? any Idea why? and i also dont get any error..

    many thanks ….

  24. @John:
    I can’t confirm that this code is ok because it seems to me that it is not complete. I’m not sure if that’s due some formating error in wordpress comments. BTW. I found BUG in this code, I mean not actually a BUG…hm…what happens is that you end up with wrong SKU once product is ordered. So in order to show SKU of simple product instead of configurable product, you should edit this file:
    app/code/core/Mage/Catalog/Model/Product/Type/configurable.php
    Of course move it in local directory or make rewrite or something 🙂
    What you should do there if I remember correctly is :
    With this code:
    public function getOrderOptions($product = null)
    {

    $options = parent::getOrderOptions($product);
    $options[‘attributes_info’] = $this->getSelectedAttributesInfo($product);

    if ($simpleOption = $this->getProduct($product)->getCustomOption(‘simple_product’)) {
    $p = new Mage_Catalog_Model_Product();
    $p->load($simpleOption->getData(‘value’));

    $options[‘simple_name’] = $p->getName();
    $options[‘simple_sku’] = $p->getSku();
    }

    $options[‘product_calculations’] = self::CALCULATE_PARENT;
    $options[‘shipment_type’] = self::SHIPMENT_TOGETHER;

    return $options;
    }

    replace corresponding method.
    I hope thats all o.O
    P.S. Regarding your code, try post it again…

  25. Hi – thanks for this. I’m trying to implement it but I’m running into problems because when I press update cart isn’t working and my selection is reset.

    Perhaps the part I’m having difficulties with in the default.phtml – would you say the following is correct?:

    getOptionList()):?>
    getProduct()->isConfigurable()){
    $_product = Mage::getModel(‘catalog/product’)->load($this->getProduct()->getId());
    Mage::getBlockSingleton(‘catalog/product_view_type_configurable’)->unsetData();
    $_configurable = Mage::getBlockSingleton(‘catalog/product_view_type_configurable’)->setData(‘product’, $_product);
    $_cdata = json_decode($_configurable->getJsonConfig());
    $_current = array();
    foreach((array)$this->getOptionList() as $_option) {
    $_current[$_option[‘label’]]=$_option[‘value’];
    }
    foreach($_cdata->attributes as $attribute) {
    ?>

    label; ?>
    <select style="width:150px;" name="cart[getId() ?>][option][id ?>]”>
    options as $option) { ?>
    <option label]==$option->label) ? ‘ selected’ : ” ?> value=”id ?>”>label ?> helper(‘checkout’)->formatPrice($option->price+$_item->getProduct()->getPrice()) ?>

    getFormatedOptionValue($_option[‘value’]) ?>
    htmlEscape($_option[‘label’]) ?>
    <dd class=”truncated”>

    htmlEscape($_option[‘label’]) ?>

    Please let me know. I urgently need this. Thanks

  26. Have i crude but working solution for Custom Options in simple products, will try to work our how we didit and publish it sometime

  27. @Ian Henshaw

    First you find this line:
    < ?php if ($_options = $this->getOptionList()):?>

    Paste given code in that “if”, and all that was in that “if”, you put where “// THIS IS PLACE WHERE EXISTING CODE” stands XD

    Regarding “Custom Options in simple products”… sorry, I can’t help you at this moment… usually we write about things we do or discover when making something for client so I don’t actually know how to do it until I work on it =_O

    @Ben: Tnx! ^_^

    @Dennis Yes I do have it, and no I can’t send link since site is not in production yet @_@

  28. what would it take to make this work with Custom Options in simple products???????

    cheers

  29. Firstley thanks for this ive been trying to work it our for days.

    just a quick question where you say

    // THIS IS PLACE WHERE EXISTING CODE from [*] goes

    down to where do i include at this point?

    cheers

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.