Hi
I created some custom product attributes programatically via the InstallData.
I managed to get them filterable and "displayable" in the product grid in the back-office catalog. However, is there a way to make them visible directly in the grid, without to have to select them in the column list?
Regards
If you created your custom attribute using the Magento\Eav\Setup\EavSetup::addAttribute() function, then you can add 'is_used_in_grid' => true in the $attr parameter.
Here is an example for the url_key attribute from the CatalogUrlRewrite module.
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'url_key',
[
'type' => 'varchar',
'label' => 'URL Key',
'input' => 'text',
'required' => false,
'sort_order' => 10,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
'used_in_product_listing' => true,
'group' => 'Search Engine Optimization',
'is_used_in_grid' => true,
'is_visible_in_grid' => false,
'is_filterable_in_grid' => true,
]
);
You may notice that 'is_visible_in_grid' is set to false for all product attributes. This value sounds like it would make your attribute visible in the grid, but it appears this may be a bug in Magento, because it does seem to work for the customers grid.
I check in the database, the status or the column is_visible_in_grid has no effect.
Below is my code:
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'discount_percentage',
[
'type' => 'decimal',
'label' => 'Discount percentage',
'input' => 'text',
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => 0,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => true,
'is_used_in_grid' => true,
'is_filterable_in_grid' => true,
'unique' => false,
'apply_to' => '',
'group' => 'General',
'system' => true
]
);