TheBritishAreComing
TheBritishAreComing

Reputation: 1717

Magento - Adding Attributes programatically with default and searchable

I've searched everywhere for the answer, muddled through Magento and I can't find a working solution. I'm creating magento attributes dynamically and that's fine, but when it comes to

nothing seems to work.

Here is my code for adding an attribute

$key = "Brand";
$name = "brand";
$specific = "Cola";
$installer->addAttribute('catalog_product', $name, array(
    'type'       => 'varchar',
    'input'      => 'select',
    'backend'           => '',
    'frontend'          => '',
    'label'             => $key,                                                    
    'class'             => '',
    'source'            => '',
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible'           => true,
    'required'          => false,
    'user_defined'      => true,
    'default'           => '',
    'searchable'        => true,
    'filterable'        => true,
    'comparable'        => true,
    'visible'      => true,
    'visible_on_front'   => true,
    'visible_in_advanced_search'   => true,                                                 
    'unique'            => false,
    'apply_to'          => '',
    'is_configurable'   => false,
    'option'        => array(
        'values'    => array($specific)
    )
));
$installer->endSetup();

$attrID = $installer->getAttribute('catalog_product', $name,'attribute_id');
$attr = Mage::getModel('eav/entity_attribute')->load($attrID);
$attr->setStoreLabels(array(1 => $key))->save();

It add's it fine, it even adds the option for me, but I can't seem to set that option as default (to add more later) and I can't make it searchable.

I really hope someone can help.

Thanks

Update:

Ok i've managed to get it adding the default (still not searchable etc) using this code.

$key = "Brand";
$name = "brand";
$specific = "Cola";
$installer->startSetup();
$installer->addAttribute('catalog_product', $name, array(
    'type'       => 'int',
    'input'         => 'select',
    'backend'           => '',
    'frontend'          => '',
    'label'             => $key,                                                    
    'class'             => '',
    'source'            => 'eav/entity_attribute_source_table',
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible'           => true,    
    'required'          => false,
    'user_defined'      => true,
    'default'           => '',
    'searchable'        => true,
    'filterable'        => true,
    'comparable'        => true,
    'visible'      => true,
    'visible_on_front'   => true,
    'visible_in_advanced_search'   => true,                                                 
    'unique'            => false,
    'apply_to'          => '',
    'is_configurable'   => false,
    'option'        => array(
        'value' => array(
                $this->getAttributeName($specific) => array($specific)
        )
    )
));
$installer->endSetup();

$attrID = $installer->getAttribute('catalog_product', $name,'attribute_id');
$attr = Mage::getModel('eav/entity_attribute')->load($attrID);
$attr->setStoreLabels(array(1 => $key));
$attr->setDefaultValue($attr->getSource()->getOptionId($this->getAttributeName($specific)));

$attr->save();

however when i go to add a new option using this code where $specific = "Pepsi";

$model = Mage::getModel('catalog/resource_eav_attribute');

$option = array();

$option['attribute_id'] = $attr;                                                        
$option['value'][$this->getAttributeName($specific)][1] = $specific;

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttributeOption($option);

I get the error: "Default option value is not defined"

Upvotes: 2

Views: 5204

Answers (3)

Dan
Dan

Reputation: 280

Referring to the error:

"Default option value is not defined"

When you add the option: ---------------------------------------> /

$option['value'][$this->getAttributeName($specific)][1] = $specific;

Here the [0] is the option's "default admin value" and [1] is the "default store value". $option['value'][$this->getAttributeName($specific)][0] = $specific;

Your code is not making $specific the default option, but setting $specific the defaults store's value.

Upvotes: 0

Krzysztof Chełchowski
Krzysztof Chełchowski

Reputation: 119

Look in to catalaog_eav_attribute. There is a column 'is_searchable' which type is smallint

Try this:

'is_searchable' => 1,

Upvotes: 0

Cristiano Casciotti
Cristiano Casciotti

Reputation: 1026

Did you have tried with 'is_searchable' => true instead of 'searchable' => true?

Upvotes: 0

Related Questions