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

SOLVED - Retrieve selected custom option from shopping cart? 
 
Seiji
Jr. Member
 
Total Posts:  9
Joined:  2008-06-28
 

After a customer adds a product to their cart, I need to figure out what custom option they selected from a drop-down list.  This is the reasoning:

I’m developing a website for a wireless provider that would allow customers to purchase new phones and plans.  Some of the plans are shared, so when they add one of these, I need to continue to redirect them to the cell phones page until they have enough phones for all lines on the plan.

I defined the plan as a virtual product, with a custom option ‘Number of lines’.  The customer selects the number from the drop-down list and adds it to the cart.  I have spent the past two days drilling down into the code and cannot figure this out.  Any help would be greatly appreciated.

Thanks,
Seiji

 
Magento Community Magento Community
Magento Community
Magento Community
 
Seiji
Jr. Member
 
Total Posts:  9
Joined:  2008-06-28
 

Well, after several days of digging through the code, I finally found what I was looking for.  I’m posting the module that contained the code and the snippet here in the hopes that others may find this useful.

The code for identifying the custom options was found in app/code/core/Mage/Checkout/Block/Cart/Item/Renderer.php

The method is getProductOptions(), and since I still don’t have a very firm grasp of how everything works in Magento/Zend I wasn’t able to instantiate this renderer object so that I could use this method, so I was reduced to copy/paste the code into my method.  Here’s the code that I am using inside the addAction() method:

$items $cart->getItems();
            
$num_phones 0;
            
$num_lines 0;

            foreach(
$items as $item{
                $cart_product 
$item->getProduct();
                
$categories $cart_product->getCategoryIds();

                foreach(
$categories as $category_id{
                    $category_name 
Mage::getModel('catalog/category')->load($category_id)->getName();
                    if(
strcasecmp($category_name'Phones & Devices') === 0{
                        $num_phones 
+= $item->getQty()*1;
                    
}
                    
else if(strcasecmp($category_name'Plans') === 0{
                        
if($options $this->_retrieve_selected_options($item)) {
                            
foreach($options as $option{
                                
if($option['label'== 'Number of Lines'{
                                    $num_lines 
+= intval($option['value']);
                                    break;
                                
}
                            }
                        }
                    }
                }
            }

            
if($num_phones && ($num_phones $num_lines)) {
                $this
->getResponse()->setRedirect('/plans');
            
}
            
else if($num_lines && ($num_lines $num_phones)) {
                $this
->getResponse()->setRedirect('/phones-devices');
            
}
            
else if (!$this->_getSession()->getNoCartRedirect(true)) {
                $this
->_getSession()->addSuccess($message);
                
$this->_goBack();
            
}
        }

And here’s the _retrieve_selected_options() method:

private function _retrieve_selected_options($item{
        $options 
= array();
        if (
$optionIds $item->getOptionByCode('option_ids')) {
            $options 
= array();
            foreach (
explode(','$optionIds->getValue()) as $optionId{
                
if ($option $item->getProduct()->getOptionById($optionId)) {
                    $formatedValue 
'';
                    
$optionGroup $option->getGroupByType();
                    
$optionValue $item->getOptionByCode('option_' $option->getId())->getValue();
                    if (
$option->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_CHECKBOX
                        
|| $option->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_MULTIPLE{
                        
foreach(split(','$optionValue) as $value{
                            $formatedValue 
.= $option->getValueById($value)->getTitle() . ', ';
                        
}
                        $formatedValue 
Mage::helper('core/string')->substr($formatedValue0, -2);
                    
elseif ($optionGroup == Mage_Catalog_Model_Product_Option::OPTION_GROUP_SELECT{
                        $formatedValue 
$option->getValueById($optionValue)->getTitle();
                    
else {
                        $formatedValue 
$optionValue;
                    
}
                    $options[] 
= array(
                        
'label' => $option->getTitle(),
                        
'value' => $formatedValue,
                    );
                
}
            }
        }
        
if ($addOptions $item->getOptionByCode('additional_options')) {
            $options 
array_merge($optionsunserialize($addOptions->getValue()));
        
}
        
        
return $options;
    
}

I hope this helps someone else!

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