I'm building a Magento shop for a customer and would like to add an attribute to all products that functions as a dropdown list. Like the 'Manufacturer' attribute.
However, instead of manually entering the list of values for the attribute I'd like the values to be populated from an external site. So, every time you create a new product or edit an existing one, the dropdown is populated with the up-to-date list of values that has been retrieved from a remote site. The list should not be fetched just once during creation of the attribute, but every time a product is created or edited.
For example, assume you can get a list of all Manufacturers via curl from a remote site. The names in this list are occasionally modified or added, however the id's stay the same so they should be used behind the scenes. How would you prefill the values of the manufacturer's dropdown?
I know how to create a basic custom extension, but extending the adminhtml backend is new stuff for me.
Any help is appreciated.
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.
Edit: 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 metadata in which case you will want to have a custom backend model.
You need to look up Magento Event Dispatchers.
Basically, every time an event occurs - updating a product, submitting an order, deleting something etc - a message is sent through the system. These messages can be caught and can then trigger methods you have created. To achieve what you want, you would need to catch the event that is triggered when a product is created and updated and get this to run a method from one of your custom modules. This method would then grab the value from the remote site and update the product with it.
After a quick Google Search, I found the following article:
You can find what Magento events are triggered by looking at the appropriate module code.