0

How can I create a new STATIC Product attribute in Magento 2?

We use barcode as a unique identifier (same as SKU) and are considering adding this - same as sku - as a STATIC attribute directly to product entity table

Why? We think this is faster during lookups ...

But we are not sure where/how this could unexpected problems

In short: How can I create a new STATIC Product attribute in Magento 2? And will this work?

0

If you want to use custom static attributes in your project, you have to do 2 things in your install/upgrade scripts.

  1. You need to add a column to the main entity table, with the correct column definition. There is not method to do this in 1 go.
  2. Install your attribute using the addAttribute() method, and define your attribute as static.

Refer to the install scripts of Mage_Catalog to better understand how things work in this case.

$installer->run("
    ALTER TABLE `{$installer->getTable('catalog/product')}` ADD `has_options` SMALLINT(1) NOT NULL DEFAULT '0';
");

$installer->addAttribute('catalog_product', 'has_options', array(
    'type' => 'static',
    'visible'=>false,
    'default' => false
));

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.