1

For my custom module when i add select box options as follows its not getting added to the database

'include_in_sitemap'        => array(
                    'type'                       => 'text',
                    'label'                      => 'Include in sitemap',
                    'input'                      => 'select',
                    'required'                   => true,
                    'sort_order'                 => 13,
                    'global'                     => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
                    'group'                      => 'Meta Information',
                    'source'                     => 'seo/source_sitemap'
                ),

But when i do as follows its getting saved but option value is set with attribute id.

'include_in_sitemap'        => array(
'type'                       => 'text',
'label'                      => 'Include in sitemap',
'input'                      => 'select',
'required'                   => true,
'sort_order'                 => 13,
'global'                     => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'group'                      => 'General',
'option'                    =>array(
    'values'    =>array(
    'Auto detect'   =>  'Auto detect',
    'Always include'    =>  'Always include',
    'Never include' =>  'Never include'                             
    )
)

),

Which is the right way to add and whats wrong with my code?I need options as <option value="text">Text</option>. Is it possible ?

1 Answer 1

0

When creating attributes this way the values you specify are included in the option values table and their ids are used as values for the dropdown.

If you want the values to be something else, you need to create a custom source model for the attribute.
You can find a way to do that in here.
In that example the type of the attribute is int, but in case you want to make it varchar see the changes in the code below.

The idea is to add the attribute like this (notice the source element. it points to a model):

$this->addAttribute('catalog_product', 'provider', array(
        'group'                => 'General',
        'type'              => 'varchar',
        'backend'           => '',
        'frontend_input'    => '',
        'frontend'          => '',
        'label'             => 'Provider',
        'input'             => 'select',
        'class'             => '',
        'source'            => '[module]/attribute_source_provider',
        'global'             => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,//can be SCOPE_WEBSITE or SCOPE_STORE
        'visible'           => true,
        'used_in_product_listing' =>true,//can also be false
        'frontend_class'     => '',
        'required'          => false,//can be true
        'user_defined'      => true,
        'default'           => '',
        'searchable'        => false,//can be true
        'filterable'        => false,//can be true
        'comparable'        => false,//can be true
        'visible_on_front'  => false,//can be true
        'unique'            => false,
        'position'            => 60,//put any number here
    ));

Then create the source model similar to this:

class Easylife_Provider_Model_Attribute_Source_Provider extends Mage_Eav_Model_Entity_Attribute_Source_Abstract{
    protected $_options = null;
    public function getAllOptions($withEmpty = false){
        if (is_null($this->_options)){
            $this->_options = array();
                       //$this->_options[] = array('label'=>'HERE GOES THE LABEL', 'value'=>'HERE GOES THE VALUE');
            //as example
            $this->_options[] = array('label'=> $this->__('Provider 1'), value=>'val1');
            $this->_options[] = array('label'=> $this->__('Provider 2'), value=>'val2');
            $this->_options[] = array('label'=> $this->__('Provider 3'), value=>'val3');
        }
        $options = $this->_options;
        if ($withEmpty) {
            array_unshift($options, array('value'=>'', 'label'=>''));
        }
        return $options;
    }
    public function getOptionText($value)
    {
        $options = $this->getAllOptions(false);

        foreach ($options as $item) {
            if ($item['value'] == $value) {
                return $item['label'];
            }
        }
        return false;
    }
    public function getFlatColums()
    {
        $attributeCode = $this->getAttribute()->getAttributeCode();
        $column = array(
            'unsigned'  => false,
            'default'   => null,
            'extra'     => null
        );

        if (Mage::helper('core')->useDbCompatibleMode()) {
            $column['type']     = 'varchar(255)';
            $column['is_null']  = true;
        } else {
            $column['type']     = Varien_Db_Ddl_Table::TYPE_TEXT;
            $column['length']   = 255;
            $column['nullable'] = true;
            $column['comment']  = $attributeCode . ' column';
        }

        return array($attributeCode => $column);
    }
    public function getFlatUpdateSelect($store)
    {
        return Mage::getResourceModel('eav/entity_attribute')
            ->getFlatUpdateSelect($this->getAttribute(), $store);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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