Sometimes you need to extend functionality of Magento 2 categories. In this article I would like to show you how to add a new custom category attribute. Let’s say, this attribute is needed to display/hide some content on the category page.
Now let’s write our installation script under Setup folder. I’ve included comments within to guide you along. Note that the custom variable I’m adding is called hide_this_category. This attribute will be used for hide particular category from menu.
In this case I’ve added a Boolean field that will be stored in used as Yes/No, but you can change this according to your requirement.
Lets check code to add custom category attribute.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | <?php namespace Hiddentechies\CategoryAttr\Setup; use Magento\Framework\Module\Setup\Migration; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Catalog\Setup\CategorySetupFactory; /** * @codeCoverageIgnore */ class InstallData implements InstallDataInterface { /** * Category setup factory * * @var CategorySetupFactory */ private $categorySetupFactory; /** * Init * * @param CategorySetupFactory $categorySetupFactory */ public function __construct(CategorySetupFactory $categorySetupFactory) { $this->categorySetupFactory = $categorySetupFactory; } /** * {@inheritdoc} * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $installer = $setup; $installer->startSetup(); $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]); $entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Category::ENTITY); $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId); $menu_attributes = [ 'hide_this_category' => [ 'type' => 'int', 'label' => 'Hide This Category', 'input' => 'select', 'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean', 'required' => false, 'sort_order' => 10, 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE, 'group' => 'Custom Attribute' ] ]; foreach ($menu_attributes as $item => $data) { $categorySetup->addAttribute(\Magento\Catalog\Model\Category::ENTITY, $item, $data); } $idg = $categorySetup->getAttributeGroupId($entityTypeId, $attributeSetId, 'Custom Attribute'); foreach ($menu_attributes as $item => $data) { $categorySetup->addAttributeToGroup( $entityTypeId, $attributeSetId, $idg, $item, $data['sort_order'] ); } $installer->endSetup(); } } |
Please setup this installation script with your extension and run upgrade command via command line. If things didn’t work out, check to see if the installation script actually ran and make sure that you cleared your cache and double-check your installation script filename, path and version number with Magento 2 extension structure.
May 7, 2020 at 2:53 pm
Thank you for this great post.