Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Change custom option of cart item
 
office309
Jr. Member
 
Total Posts:  6
Joined:  2012-02-20
 

I’ve searched the internet to find a working solution, but without any luck. What I’m trying to do, via an event, is to update a custom option of a simple product. But the changes just don’t want to be saved.

This is the latest code:

$_cart Mage::getSingleton'checkout/cart' );

        
// I'm retrieving the number somewhere (not important for the problem)
        
$_product_option_value_id 123;

        
// loop items
        
foreach ( $_cart->getItems() as $_item )
        
{
            
// check product ID
            
if ( $_item->getProductId() == $this->_product_id )
            
{
                
// get item options
                
$_item_options $_item->getOptions();

                
// loop
                
foreach ( $_item_options as $_item_option )
                
{
                    
// check code
                    
if ( $_item_option->getCode() == 'info_buyRequest' )
                    
{
                        
// unserialize value
                        
$_cart_item_option_value unserialize$_item_option->getValue() );
                        
// set ID
                        
$_cart_item_option_value[ 'options' ][ $this->_product_option_id ] = (string) $_product_option_value_id;
                        
// serialize and add option
                        
$_item_option->setValueserialize$_cart_item_option_value ) );
                    
}
                    
elseif ( $_item_option->getCode() == 'option_' $this->_product_option_id )
                    
{
                        
// set ID
                        
$_cart_item_option_value[ 'value' = (string) $_product_option_value_id;
                        
// serialize and add option
                        
$_item_option->setValue$_product_option_value_id );
                    
}

                    
// save
                    
$_item_option->save();

                    
// add option
                    
$_item->addOption$_item_option );
                
}
            }

            
// save
            
$_item->save();
        
}

        
// save
        
$_cart->save();
 
Magento Community Magento Community
Magento Community
Magento Community
 
jeffcustom
Member
 
Avatar
Total Posts:  67
Joined:  2012-11-29
Boulder, CO
 

Your code looks correct.

You should only need to save the item option --- you shouldn’t need to call $_item->addOption over again.

What event are you hooking into?

Once again, your code should be working, but if this is a critical issue you could always resort to raw sql:

$table =  Mage::getSingleton('core/resource')->getTableName('sales_flat_quote_item_option');
$resource Mage::getSingleton('core/resource');
$readConnection $resource->getConnection('core_read');

$readConnection->query('UPDATE ' $table ' SET value = "' $value '" WHERE item_id = ' $itemid ' AND code = "$code");
 
Magento Community Magento Community
Magento Community
Magento Community
 
office309
Jr. Member
 
Total Posts:  6
Joined:  2012-02-20
 

Sorry for the late response..

I’m hooking into sales_quote_save_after event. But I will try your sql solution wink

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top