0

How to get all option of attributes in magento, ? I want to get all options of color attribute.

 $swatches = Mage::getStoreConfig('configswatches/general/swatch_attributes');

    echo "<pre>";print_R($swatches); die;

    output: 92,223,81,221,180

I got color attribute id = 92.. but how to load this id to get all options of color attributes.?

0
7

You can try below code:

 $name='whatever_your_attribute_name';
 $attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter($name)->getFirstItem();
 $attributeId = $attributeInfo->getAttributeId();
 $attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
 $attributeOptions = $attribute ->getSource()->getAllOptions(false); 
 print_r($attributeOptions);
3

Use below code to get all options of attribute with Id :

<?php
$attribute = Mage::getSingleton('eav/config')
    ->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'color');

if ($attribute->usesSource()) {
    $options = $attribute->getSource()->getAllOptions(false);
    echo "<pre>";
    print_r($options);
    echo "</pre>";
}
?>

This will output/prints options Array like :

Array
(
    [0] => Array
        (
            [value] => 20
            [label] => Black
        )

    [1] => Array
        (
            [value] => 27
            [label] => Blue
        )

    [2] => Array
        (
            [value] => 221
            [label] => Brown
        )

    [3] => Array
        (
            [value] => 17
            [label] => Charcoal
        )

    ....

UPDATE : For key=>value pairs, use array_column function,

Just replace print_r($options); TO print_r(array_column($options, 'label', 'value'));

2
  • how to change these array in this format : array( 'key'=> 'Color', 'value' => 'blue', 'img' => 'blue.png', ); ... i mean how to add extra key through coding.. – Magento 2 Learner Aug 5 '16 at 6:57
  • check updated answer – Anil Suthar Aug 5 '16 at 7:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.