cancel
Showing results for 
Search instead for 
Did you mean: 

Update item options on cart page in magento

SOLVED

Update item options on cart page in magento

 
1 ACCEPTED SOLUTION

Accepted Solutions

Re: Update item options on cart page in magento

You need to follow following steps to create a observer for updating item size on shopping cart page:

Naming convention in code

Namespace - Bws
Module Name - Editsize

Step 1) Create main module xml file

We need to create a file called Bws_Editsize.xml in app/etc/modules.

 

<?xml version="1.0"?>
<config>
    <modules>
        <Bws_Editsize>
            <codePool>local</codePool>
            <active>true</active>
        </Bws_Editsize>
    </modules>
</config>

This file allows Magento to recongise the modules existence and is where we can deactivate it (by changing <active>true</active> to <active>false</active>)

Step 2) Create folder structure for our module

We need to create the folders where our modules code will live. To do this create the following directories:

mkdir app/code/local/Bws/
mkdir app/code/local/Bws/Editsize/
mkdir app/code/local/Bws/Editsize/etc
mkdir app/code/local/Bws/Editsize/Model

Quick explaination of the module folder structure. Bws represents the company or group, then Editsize is our actual module. etc will contain various configuration files which instructs Magento how to read/use our module. Model contains our various Model classes which our module might use (in this case we will have a Observer.php file in the Model folder).

Step 3) Create our module’s configuration file

 Each module requires a file called config.xml this file lives in app/code/local/Bws/Editsize/etc

<?xml version="1.0"?>
<config>
    <modules>
        <Bws_Editsize>
            <version>0.0.1</version>
        </Bws_Editsize>
    </modules>
    <global>
        <models>
            <bwseditsize>
                <class>Editsize_Model</class>
            </bwseditsize>
        </models>
        <events>
                <checkout_cart_update_items_after>
                        <observers>
                                <update_items_specific_option>
                                        <class>Bws_Editsize_Model_Observer</class>
                                        <method>updateItemsSpecificOption</method>
                                </update_items_specific_option>
                        </observers>
                </checkout_cart_update_items_after>
        </events>
    </global>
</config>

The above code is our basic config for our model. If you stick to similar naming conventions you’ll get on by fine here.

The main code I’d like to highlight here is contained between the <events> tags. The first tag in represents our event we decided to use earlier on checkout_cart_update_items_after, so within here we are defining what to do when that event is fired. Within the <observers> tag we have the set up for our Observer. The value within <class> represents the class name of our Observer, and then the value in <method> is the method (or function) we will run when that event is fired.

Step 4) Creating our Observer.php

Now we can create our Observer and place our code inside our method we will create. To do this create a file named  Observer.php in app/code/local/Bws/Editsize/Model and place the following code:

<?php
class Bws_Editsize_Model_Observer {
    
    /**
        $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)
    */
    public function updateItemsSpecificOption(Varien_Event_Observer $observer) {
        
        $aPost  = Mage::app()->getFrontController()->getRequest()->getPost();;
        #print_r($aPost);
        $quoteItems = $observer->getCart()->getQuote()->getAllVisibleItems();
        foreach ($quoteItems as $item) {
            
            $cart_arr   =   $aPost['cart'];
            
            $itemId = $item->getId();
            $productId = $item->getProductId();
            $productQty = $item->getQty();
            $option_arr     =   $cart_arr[$itemId]['option'];
            foreach($option_arr as $key=>$val_option) {
                $superAttributeId = $key;
                $childId = $val_option;
            }
            
            
            #$refresh = $this->getRequest()->getParam('refresh');

            $params = array(
                'id' => $itemId,
                'qty' => $productQty,
                'product' => $productId,
                'super_attribute' => array($superAttributeId => $childId)
            );


            $cart = Mage::getSingleton('checkout/cart');
            $quoteItem = $cart->getQuote()->getItemById($itemId);
            if (!$quoteItem) {
                Mage::throwException($this->__('Quote item is not found.'));
            }

            $item = $cart->updateItem($itemId, new Varien_Object($params));
            if (is_string($item)) {
                Mage::throwException($item);
            }
            $cart->save();

            Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
            
        }
    }
}
?>

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)
 
Step 5) Update default.phtml

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

Done Smiley Happy

View solution in original post

3 REPLIES 3

Re: Update item options on cart page in magento

I got solution for it using Observer on "checkout_cart_update_items_after" event.

 

Will post complete solution within 2 days after testing.

Re: Update item options on cart page in magento

You need to follow following steps to create a observer for updating item size on shopping cart page:

Naming convention in code

Namespace - Bws
Module Name - Editsize

Step 1) Create main module xml file

We need to create a file called Bws_Editsize.xml in app/etc/modules.

 

<?xml version="1.0"?>
<config>
    <modules>
        <Bws_Editsize>
            <codePool>local</codePool>
            <active>true</active>
        </Bws_Editsize>
    </modules>
</config>

This file allows Magento to recongise the modules existence and is where we can deactivate it (by changing <active>true</active> to <active>false</active>)

Step 2) Create folder structure for our module

We need to create the folders where our modules code will live. To do this create the following directories:

mkdir app/code/local/Bws/
mkdir app/code/local/Bws/Editsize/
mkdir app/code/local/Bws/Editsize/etc
mkdir app/code/local/Bws/Editsize/Model

Quick explaination of the module folder structure. Bws represents the company or group, then Editsize is our actual module. etc will contain various configuration files which instructs Magento how to read/use our module. Model contains our various Model classes which our module might use (in this case we will have a Observer.php file in the Model folder).

Step 3) Create our module’s configuration file

 Each module requires a file called config.xml this file lives in app/code/local/Bws/Editsize/etc

<?xml version="1.0"?>
<config>
    <modules>
        <Bws_Editsize>
            <version>0.0.1</version>
        </Bws_Editsize>
    </modules>
    <global>
        <models>
            <bwseditsize>
                <class>Editsize_Model</class>
            </bwseditsize>
        </models>
        <events>
                <checkout_cart_update_items_after>
                        <observers>
                                <update_items_specific_option>
                                        <class>Bws_Editsize_Model_Observer</class>
                                        <method>updateItemsSpecificOption</method>
                                </update_items_specific_option>
                        </observers>
                </checkout_cart_update_items_after>
        </events>
    </global>
</config>

The above code is our basic config for our model. If you stick to similar naming conventions you’ll get on by fine here.

The main code I’d like to highlight here is contained between the <events> tags. The first tag in represents our event we decided to use earlier on checkout_cart_update_items_after, so within here we are defining what to do when that event is fired. Within the <observers> tag we have the set up for our Observer. The value within <class> represents the class name of our Observer, and then the value in <method> is the method (or function) we will run when that event is fired.

Step 4) Creating our Observer.php

Now we can create our Observer and place our code inside our method we will create. To do this create a file named  Observer.php in app/code/local/Bws/Editsize/Model and place the following code:

<?php
class Bws_Editsize_Model_Observer {
    
    /**
        $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)
    */
    public function updateItemsSpecificOption(Varien_Event_Observer $observer) {
        
        $aPost  = Mage::app()->getFrontController()->getRequest()->getPost();;
        #print_r($aPost);
        $quoteItems = $observer->getCart()->getQuote()->getAllVisibleItems();
        foreach ($quoteItems as $item) {
            
            $cart_arr   =   $aPost['cart'];
            
            $itemId = $item->getId();
            $productId = $item->getProductId();
            $productQty = $item->getQty();
            $option_arr     =   $cart_arr[$itemId]['option'];
            foreach($option_arr as $key=>$val_option) {
                $superAttributeId = $key;
                $childId = $val_option;
            }
            
            
            #$refresh = $this->getRequest()->getParam('refresh');

            $params = array(
                'id' => $itemId,
                'qty' => $productQty,
                'product' => $productId,
                'super_attribute' => array($superAttributeId => $childId)
            );


            $cart = Mage::getSingleton('checkout/cart');
            $quoteItem = $cart->getQuote()->getItemById($itemId);
            if (!$quoteItem) {
                Mage::throwException($this->__('Quote item is not found.'));
            }

            $item = $cart->updateItem($itemId, new Varien_Object($params));
            if (is_string($item)) {
                Mage::throwException($item);
            }
            $cart->save();

            Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
            
        }
    }
}
?>

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)
 
Step 5) Update default.phtml

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

Done Smiley Happy

Re: Update item options on cart page in magento

Could you please give solution for magento 2.1.8?