LinkedIn Twitter RSS Reset

Get all options of select or multiselect attribute in Magento by attribute code

One of the things that a Magento developer deal, time and again is finding out all the options of color or manufacturer or size attribute. You may also need to find out what are the options available for color attribute before you add new option programmatically. Or you may need to add assign the option’s ID to a product. Here’s what you need to do, just load the attribute by attribute code, then load all its options. Easy, isn’t it! Code is below.


// Add the attribute code here.
$attribute=$product->getResource()->getAttribute("color");

// Checking if the attribute is either select or multiselect type.
if($attribute->usesSource()){

	// Getting all the sources (options) and print as label-value pair
	$options = $attribute->getSource()->getAllOptions(false);

	echo "<pre>";
	print_r($options);
}

11 Responses to “Get all options of select or multiselect attribute in Magento by attribute code”

  1. Magento Fan
    January 2, 2010 at 9:33 pm #

    Thanks for the tip Subesh. I’ve been having some issues with a similar issue. Whenever I try to list or filter “multiple select” based attributes, I get a blank page. The following based on a “textfield” attribute for example, works fine. Whenever I enter the code of a “multiple select” attribute, it stops from working. I appreciate if you can pass a hint of a solution. Happy New Year!

    getCollection();
    $collection->addAttributeToSelect(‘name’); //multiple select attributes fail
    $collection->load();
    ?>

    <a href="getProductUrl() ?>”>getName() ?>

  2. Magento Fan
    January 2, 2010 at 9:40 pm #

    One more time. Really sorry. Don’t know how enclose the code. Not working.

    $model = Mage::getModel(‘catalog/product’);
    $collection = $model->getCollection();
    $collection->addAttributeToSelect(‘name’); //multiple select attributes fail
    $collection->load();

    • Subesh Pokhrel
      January 4, 2010 at 4:32 am #

      @Magento Fan

      $collection = $model->getCollection()
      ->addAttributeToSelect(“color”) ->addAttributeToFilter(“color”,’15′);
      $collection->load();

      You can work like this..

  3. Magento Question
    February 23, 2010 at 9:20 am #

    @subesh
    Refering to the code you posted first.
    Is there also a way to not show all the attributes of a product?
    Or better in my case, Only the attribute ‘size’ that is out of stock?

    best regards

    • Subesh Pokhrel
      February 23, 2010 at 10:14 am #

      In Frontend? or Backend…

  4. him
    March 14, 2010 at 4:56 pm #

    // Add the attribute code here.
    $attribute=$product->getResource()->getAttribute(“color”);

    // Checking if the attribute is either select or multiselect type.
    if($attribute->usesSource()){

    // Getting all the sources (options) and print as label-value pair
    $options = $attribute->getSource()->getAllOptions(false);

    echo “”;
    print_r($options);
    }

    form this i am egtting pair of option but I need only value of option..what i will do

    • Subesh Pokhrel
      March 15, 2010 at 12:12 pm #

      This should fix your problem

      $_attribute=$_product->getResource()->getAttribute(“size”);
      $_sourceTable = Mage::getModel(‘eav/entity_attribute_source_table’)->setAttribute($_attribute);
      $_sizeText = $_sourceTable->getOptionText($_product->getSize());

  5. Sumanta Pati
    March 27, 2010 at 6:15 am #

    Thank You . It’s solve my problem

  6. Remon
    May 19, 2011 at 10:16 am #

    Hi there.
    Your code solved some for us, but not completely. Say we have an optionlist size that has S M L and XL. Some shirts are only entered in the shop with a M and L variant.
    When we use your code on the category page all options are listed (S thru XL) and not only the M and L.
    We’ve been googling for 3 days now, we’re at a loss. Maybe you can point us in the right direction?
    Thanks in advance!

  7. Justin
    March 30, 2012 at 4:18 pm #

    I tried your code and its work but I want only those color which are assigned to a particular product.

    Thanks.

  8. Justin
    March 30, 2012 at 4:25 pm #

    Ohh yes

    I have found it my own.

    This code works for fetching the associated colors for a product in product listing or category listing.

    foreach ($_product->getOptions() as $o) {
    if($o->getTitle() == ‘Available Colors’){
    $values = $o->getValues();
    foreach ($values as $v) {
    echo ‘, ‘.$v->getData(‘default_title’) ;
    }
    }
    }

    Enjoy!!!

Leave a Comment