How to get configurable product options and attributes

How to get configurable product options and attributes

Getting all options of a specific attribute in Magento is fairly easy:

$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'color');  
foreach ($attribute->getSource()->getAllOptions(true) as $option) {
    echo $option['value'] . ' ' . $option['label'] . "\n";

A more challenging task is to get the options and values that are applicable to a particular configurable product. This can solved using the following code:

// Collect options applicable to the configurable product
$productAttributeOptions = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product);
$attributeOptions = array();
foreach ($productAttributeOptions as $productAttribute) {
    foreach ($productAttribute['values'] as $attribute) {
        $attributeOptions[$productAttribute['label']][$attribute['value_index']] = $attribute['store_label'];

The resulting array will consist of an associative array, indexed by the attribute label, with nested arrays containing attribute value => attribute label items.
Something like:

$attributeOptions = array(
    'Color' => array(
         1 => 'Green',
         2 => 'Orange',
    'Size' => array(
        6 => 'L',
        7 => 'XL',

Stay tuned for more Magento code tips from Atwix developers!

Read more:


Smart Brands Choose Us.

From Mark Cuban and Sir Richard Branson backed startups, to Inc 500 US Fastest Growing Companies and Global Brands choose us to deliver and support unparalleled eСommerce experience for their customers.