|
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 > 0 && ($num_phones > $num_lines)) { $this->getResponse()->setRedirect('/plans'); } else if($num_lines > 0 && ($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($formatedValue, 0, -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($options, unserialize($addOptions->getValue())); } return $options; }
I hope this helps someone else!
|