Sorry, nothing in cart.
If you are looking to pre-populate product drop down attributes (custom types) i.e. “Partners” attribute etc. then you must be wondering how to pre-populate the product drop down attribute values in that attribute. One option is to manually enter all the data which is time consuming specially if you are supposed to fill large data set. What if the source of the data is large enough to enter and update? Situation gets worse if you are asked to check whether the data is upto-date or not.
Lets take a scenario where people are coming to a separate part of your website and registering as a partner which is getting stored in other database (Non-Magento database).
Now, we have to fetch partners from this remote site and pre-populate (upto-date) data in our newly created custom attribute dropdown (partners).
Magento product attributes are extremely flexible, but having dynamic values per product would mean you would need one attribute per product which doesn’t make much sense so I am assuming that these attribute values share some things in common among products in your store so you will have a limited number of attributes (or one?). For each such attribute you should create the attribute and the backend model for it beforehand and then use a cron job or event update the attribute value options from your API and cache them locally in the database or some other method. You can add an attribute in your module installer like this:
$installer->addAttribute('catalog_product', 'my_attr_code', array( 'label' => 'Attribute Name', 'required' => false, 'input' => 'select', 'source' => 'namespace/source', 'default' => 'none', 'position' => 1, 'sort_order' => 3, ));
Or roll your own SQL insert statement for the
eav_attribute table. Make sure to assign your new attribute to an attribute set in the backend so that it is usable with your products. Then you create a model at
My_Namespace_Model_Source which extends
Mage_Eav_Model_Entity_Attribute_Source_Table (assuming you want a select input for your attribute values) and implements at minimum the following methods:
So these would pull from the dynamic options that are cached locally and for the most part your new attribute will behave like any other in Magento. Be sure to use intelligent caching because these methods could potentially get hit many times in one page load. There may be an event you could observe that is called right before a product create or edit page is displayed, otherwise just override a class or controller or use a Magento cron task.
You could also use the existing Magento attribute models and create your option values through the existing API but I assume that to perform syncing or for some other reason you may need some additional meta-data in which case you will want to have a custom back-end model.
I would love to hear your opinion and experiences of solving this problem. Subscribe our RSS to receive latest Magento updates.