Search code examples
magento

How to create custom source model for custom attribute type select?


I tried to search this but couldn't find any. When creating a custom product attribute with select type programmatically, Magento always assigns eav/entity_attribute_source_table as the source model.

There are 2 issues with this default source model:

  1. I can't auto populate the field with data taken programmatically from somewhere else other than have to type the data list manually one by one.

  2. Although I have specified the "default" or "default_value" (I can see in the database that the value is there), the field is still showing empty as the first line.

How I can change the default source_model to my own source model for select type?

Thank you


Solution

  • There is a great example of this in Mage_Customer, installer: mysql4-upgrade-1.5.9.9-1.6.0.0.php

    In it, a country source model is being assigned to customer address attribute country_id.

    $installer->updateAttribute(
        'customer_address',
        'country_id',
        'source_model',
        'customer/entity_address_attribute_source_country'
    );
    

    Change this to catalog_product, your attribute, and source model.