Feb 7, 2013

Magento: Add product custom attribute options dynamically

Once you have the product attributes in place, and you want to add lot of options to these attributes it becomes time consuming and hectic task. If you have only few attribute options to add, it’s easy to go to backend and add manually. But if you have many attribute options, more than 10 options for many attributes, it’s very tiresome and feels like data entry job. Magento is not built to do stuff manually, so here I will provide you the code snippet which will push all the attribute options to their respective attribute.

Create a file in the root of your Magento installation and copy this code there. I generally name it test.php

require_once 'app/Mage.php';

$installer = new Mage_Eav_Model_Entity_Setup('core_setup');

//Change this....
$attrCode = 'color'; //this should be there already

$aColors = array_map('utf8_encode',$aColors); //If you're using special characters
$iProductEntityTypeId = Mage::getModel('catalog/product')->getResource()->getTypeId();
$aOption = array();
$aOption['attribute_id'] = $installer->getAttributeId($iProductEntityTypeId, $attrCode);

   $aOption['value']['option'.$iCount][0] = $aColors[$iCount];


Now you just need to give the existing product attribute name AND future attribute options to this script. Run it and rest all script will do for you! 🙂


  • I have a question about Custom Attribute.

    how apply a custom option to an entire category of products in Magento?

    Don’t need configurable products, need custom option per category.

    Thanks for your Help.

  • Explain me, first for the created custom attributes like your script but divided for categories.

    Second, if exist one product with 3 custom atributes color, size, large, and i want delete color for all categorie what can i do? Any idea is important.

    Thanks a lot.

  • I’m creating copies of attribute sets for various products because their marketing color names vary across all the products.

    I’ve done two ranges of products and already I have about 20 attribute sets for 20 configurable products for the sole purpose of listing the right variations of colors for the respective products and I’ve done about 20% of all products.

    I’m sure I’m going about this the wrong way.

    It would make more sense to have a table of products and a comma delineated list for each color which is generated in admin when I create a product. This would then mean I have one single configurable product attribute set.

    Also their would need to be another table for the filter so that I have a general color list where for each color I have a corresponding comma delineated list of equivalent colors
    for eg.
    White | “Arctic, Cream. Off White, Snow, Bone”
    When white is selected in the filter any corresponding colors from the table would generate a match and the respective products would be displayed.

    This should be a relatively common issue yet I’ve found nothing on the subject. What/Where should I be searching for – What am I missing


  • Suppose I have attribute Color and dropdown value I added is WHite and Green. Now, when I push data through product creation API, color comes Black. Here I want that Black color should automatically add in COlor dropdown and my product is created successfully with Black color. –
    Can you help me to get api which can help me in this adding option dynamically, basically get attribute values and post attribute value calls

Leave a comment


Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!




Magento top 50 contributors

Magento top 50 contributors