Updating options of configurable product that is already in the cart

15 Comments 9th JUN 2009 | Posted by Tomas Novoselic in Magento

Updating options of configurable product that is already in the cart

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

If you like what you read, please share it.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Yahoo! Bookmarks
  • Reddit
  • Technorati
  • Twitter
  • StumbleUpon
  • LinkedIn
  • Netvibes
  • NewsVine
  • Sphinn
  • Tumblr
  • Posterous

To post code in comments, place your code inside [code] and [/code] tags.

There are 15 comments (Add Yours +)

  • Ian Henshaw Says

    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

  • ian henshaw Says

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

    cheers

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

  • @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 @_@

  • ian henshaw Says

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

  • 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

  • @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…

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

  • 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!

  • Has anyone been able to make this work?

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

  • 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

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

    Thanks

Leave a Comment

Please wrap all source codes with [code][/code] tags.
Magento Design and Development | Magento SEO | iPhone Application Development Web Application Development with ZEND | WordPress Ecommerce | WordPress development
Sitemap

Inchoo - webappsolutions | 2009