Magento – Get the Chosen Configuration / Options of the Products in the Cart

A Magento client of mine wanted to be able to change the calls to action on the cart and checkout pages based on the product configuration that the customers have selected for purchase. The following bit of code loops through the products in the Magento shopping basket and gets the chosen custom options and / or configuration.

[php]
// Load the session
$session = Mage::getSingleton(‘checkout/session’);
// Array to hold the final result
$finalResult = array();
// Loop through all items in the cart
foreach ($session->getQuote()->getAllItems() as $item)
{
// Array to hold the item’s options
$result = array();
// Load the configured product options
$options = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
// Check for options
if ($options)
{
if (isset($options[‘options’]))
{
$result = array_merge($result, $options[‘options’]);
}
if (isset($options[‘additional_options’]))
{
$result = array_merge($result, $options[‘additional_options’]);
}
if (!empty($options[‘attributes_info’]))
{
$result = array_merge($options[‘attributes_info’], $result);
}
}
$finalResult = array_merge($finalResult, $result);
}
// Now you have the final array of all configured options
Zend_Debug::dump($finalResult);
[/php]

Source: the function – getOrderOptions() found in the file – app/code/core/Mage/Bundle/Block/Adminhtml/Sales/Order/View/Items/Renderer.php

The options will be stored in the keys ‘label’ and ‘value’ in the resulting array.