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);
}

© Subesh Pokhrel's Blog – Magento Development Tips,PHP,Google Maps

  • Share/Bookmark

Related Post

Tags: , , ,

8 Responses


  1. Magento Fan on 02 Jan 2010

    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 on 02 Jan 2010

    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 on 04 Jan 2010

      @Magento Fan

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

      You can work like this..

  3. Magento Question on 23 Feb 2010

    @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 on 23 Feb 2010

      In Frontend? or Backend…

  4. him on 14 Mar 2010

    // 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 on 15 Mar 2010

      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 on 27 Mar 2010

    Thank You . It’s solve my problem


Leave your comment


Search engine optimization by SEO Design Solutions