5

I'm trying to setup attribute-sets and attributes automatically via a setup script. The script is working and all attributes are added to the sets, no problem with that... however, when I look at the attributes the visible_on_front, the used_in_product_listing and the global are not set properly. This is what I have:

$installer->addAttribute('catalog_product', '<attribute_code>', array(
    'group'         =>  'General',
    'input'         =>  'date',
    'type'          =>  'datetime',
    'label'         =>  '<some_label>',
    'backend'       =>  'eav/entity_attribute_backend_datetime',
    'is_global'     =>  0,
    'global'        =>  Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE,
    'is_visible_on_front'       => 1,
    'visible_on_front'          => 1,
    'used_in_product_listing'   => 1,
));

Anyone know how I can fix this so it works?

1 Answer 1

20

The trick here is to make sure that you are using the correct Setup object. The default Setup object is Mage_Eav_Model_Entity_Setup which will add your attribute into eav_attribute table but it is not aware of the extra fields in catalog_eav_attribute such as used_in_product_listing (or customer_eav_attribute and it's fields for that matter).

So, add this at the top of the install script:

$installer = new Mage_Catalog_Model_Resource_Eav_Mysql4_Setup('core_setup');
$installer->startSetup();

That should make the difference.

FYI, you can use Mage_Customer_Model_Entity_Setup to achieve the same end for customer attributes.

Sign up to request clarification or add additional context in comments.

2 Comments

Brilliant, this fixed my problem! You are the man Jonathan Day.
This answer is now out of date as of Magento 1.9.1, possibly earlier, because the $addAttribute method in an install script does now set used_in_product_listing without having to get a different object.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.