Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento module setup/installer script

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?

like image 203
JNDPNT Avatar asked Jan 24 '12 16:01

JNDPNT


People also ask

What is setup script in Magento 2?

Save. As the name defines: Setup script files used to perform some action on your data or table while installing or upgrading up your module. All the setup scripts files are created under Setup folder which is at path: app/code/Webkul/Test/Setup.

What is recurring script for Magento schema?

The Recurring script is a new feature introduced in Magento 2. It runs whenever magento setup:upgrade command is called in CLI. It works just like the Upgrade script but doesn't have a version comparison condition. The Recurring script executes whenever the module version is incremented.

How do I install Magento modules?

Navigate to the root directory of Magento 2 store using command cd /magento , where /magento - is the Magento installation directory address. Verify that your server has compatible Composer version by executing the command composer --version . If your Magento version is below 2.4.


1 Answers

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.

like image 166
Jonathan Day Avatar answered Sep 22 '22 13:09

Jonathan Day