How to Properly Loop Through a Configurable Products Super Attributes in Magento

I have run into the problem where I wanted to get all of a products super attributes and display them in my own custom way so today I am going to show you how to do it.

Once you have your current product which in this case we are going to call $_product all you have to do is the following

$_attributes = $_product->getTypeInstance(true)->getConfigurableAttributes($_product);

foreach($_attributes as $attribute)
    $attributeCode = $attribute->getProductAttribute()->getAttributeCode();

    $_allSubProducts = $_product->getTypeInstance(true)->getUsedProducts(null, $_product);

    foreach($_allSubProducts as $_subProduct)
        if ($_subProduct->isSaleable())
            $label = $_subProduct->getAttributeText($attributeCode);
            $value = $_subProduct->getData($attributeCode);

            // Your code goes here

The first thing you are doing is getting all of the super attributes. You then use a foreach loop to go through all of them. While you are doing that you are getting all of your sub-products which would be your simple products. With this you check if it is currently sellable because if it is not you don’t want it to show up. You then get the attributes label and value then display it however you need to. I usually use this technique for size and color on products because I can make it look more interesting then just a dropdown menu.

As always give me your feedback and let me know if you have another way of doing it.