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. :)

31
Top

Enjoyed this post?

Subscribe to our RSS Feed, Follow us on Twitter and spread it to your friends!

Author

Tomas Novoselic

Team Leader / Senior Developer

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

Other posts from this author

Discussion 31 Comments

Add Comment
  1. Ian Henshaw

    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

  2. ian henshaw

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

    cheers

  3. Ben

    Awesome!

  4. Dennis

    Do you have a demo of this working on a page?

  5. @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 @_@

  6. ian henshaw

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

  7. John

    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

  8. @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…

  9. vas

    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 ….

  10. John

    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!

  11. John

    Has anyone been able to make this work?

  12. @John
    Since you can display options in cart, problem must be in some other file than template you have sent :)

  13. 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

  14. I wonder how this dude managed to pack this without problems here:
    http://www.magentocommerce.com/extension/2042/edit-configuration-product-in-cart

  15. Does anyone know how I can go into an already configured product and add some more product options ?

    Thanks

  16. forestfire

    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)?

  17. @forestfire That code is about 2 years old. I don’t think it is a good idea to edit only default.phtml I guess nothing will happen when you update cart. Anyway, based on this article, someone made extension long time ago and you can find it here: http://www.magentocommerce.com/magento-connect/magestore/extension/2042/edit-configuration-product-in-cart I guess that would be safest way to go. I hope that will work for you.

  18. alex

    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.

  19. Divya

    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));

  20. 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

  21. I just want to display options price and name? any idea?
    thanks!

  22. 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 “”;
    }

  23. deepa

    Hello Tomas,
    Thanks for code.

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

  24. Viswanathan

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

  25. Arham

    hello bodiandras!

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

    thanks….

  26. 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;

  27. At response xml the comment system has eaten up my < and > s.

  28. 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

  29. Michael

    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

  30. Piet Janssen

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

    Regards,
    Piet

  31. krupa parikh

    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?

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top