Updating options of configurable product that is already in the cart

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) {
</select>
<select style="width: 150px;" name="cart[<?php echo $_item->getId() ?>][option][< ?php echo $attribute->id ?>]">?>
<option>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"?>
 
 
 
 
 
 
singleton
YOUR_FIRM_MODULE_NAME_Model_Card
update

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"?>
 
 
 
local
true

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

You made it all the way down here so you must have enjoyed this post! You may also like:

Magento vs. Shopify: Battle of the Titans Lucija Majstrovic

Magento vs. Shopify: Battle of the Titans

Group your Shipping Methods by Carrier on Checkout Danijel Vrgoc
Danijel Vrgoc

Group your Shipping Methods by Carrier on Checkout

Programmatically create a configurable Magento product Petar Sambolek
Petar Sambolek

Programmatically create a configurable Magento product

Tell us about your project

Drop us a line. We'd love to know more about your project.