1

I have a non-Eav table. And I want to create a custom product attribute with select options whose values will be coming from the above non-eav table column. What will be the code for the sql script. Assume the column name is brand.

<?php

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$setup->addAttribute('catalog_product', 'brand', array(
    'group' => 'General Information',
    'input' => 'select',
    'type' => 'varchar',
    'label' => 'Brand',
    'visible' => 1,
    'required' => 0,
    'user_defined' => 1,
    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE ,
));
1

Use below code:

$setup->addAttribute('catalog_product', "brand", array(
    'type'       => 'int',
    'input'      => 'select',
    'label'      => 'Select brand',
    'sort_order' => 1000,
    'required'   => false,
    'global'     => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'backend'    => 'eav/entity_attribute_backend_array',
    'option'     => array (
        'values' => array(
            0 => 'Brand 1',
            1 => 'Brand 2',
            2 => 'Brand 3',
        )
    ),

));
  • I can't use the predefined values as my non-eav table column will contain different values by admin. Example: Nike,Calpol,Hyundai, etc. – Akash Mar 15 '16 at 6:10
  • Works like a charm on Magento 1.9 – Anse Dec 23 '19 at 14:40
1

When an attribute has enumerated values as options, it has select as its input type (which you have in your post), and it needs a source model (source).

You need a custom source model which subclasses your brand entity's collection class and defines the toOptionArray() method, mapping both the PK and brand name columns, e.g.:

public function toOptionArray()
{
    return $this->_toOptionArray('brand_id', 'name');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.