How to Get Select/Multiselect Attribute’s Options in Magento?

Magento products can have many numbers of attributes with different input types (Text Field, Textarea, Yes/No, Dropdown, Multiple Select etc). While developing a Magento website, it’s very often to get attribute’s option if it is a select/multiselect attribute type. In this post, I will show you how to get select/multiselect attribute’s options in mMagentoeasily.

If it’s a product attribute, use the below code to get attribute label and value:

$attributeCode = "custom_color"; 
$attributeDetails = Mage::getSingleton("eav/config")->getAttribute("catalog_product", $attributeCode);
$options = $attributeDetails->getSource()->getAllOptions(false);
foreach($options as $option){ 
    echo $option["value"]; 
    echo $option["label"]; 
}

Example:

<select id="custom_color" name="custom_color">
    <option value=""><?php echo $this->__('Select Color') ?></option>
    <?php
        $attributeCode = "custom_color"; 
        $attributeDetails = Mage::getSingleton("eav/config")->getAttribute("catalog_product", $attributeCode);
        $options = $attributeDetails->getSource()->getAllOptions(false);
        foreach($options as $option){ 
        ?>
            <option value="<?php echo $option["value"]; ?>"><?php echo $option["label"]; ?></option>
        <?php 
        } 
    ?>
</select>

If it’s a customer attribute, use the below code to get attribute label and value:

$attributeCode = "profession"; 
$attributeDetails = Mage::getSingleton("eav/config")->getAttribute("customer", $attributeCode);
$options = $attributeDetails->getSource()->getAllOptions(false);
foreach($options as $option){ 
    echo $option["value"]; 
    echo $option["label"]; 
}

Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *