3

I want to add a new text area to my categories in Magento2, similar to this, but its for Magento 1: adding custom drop down field to category

Basically there should be a field for each category where my customer can put in some category-based SEO content. I want to echo it later on the sidebar.

How could I achieve this? Bonus points if I can achieve this with pure XML.

0

You need to create a module that has Setup/InstallData.php with content similar to

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);
        $categorySetup->removeAttribute(\Magento\Catalog\Model\Category::ENTITY, 'bottom_description');
        $categorySetup->addAttribute(
            \Magento\Catalog\Model\Category::ENTITY, 'bottom_description', [
                'type' => 'text',
                'label' => 'Bottom description',
                'input' => 'textarea',
                'required' => false,
                'sort_order' => 101,
                'visible' => true,
                'wysiwyg_enabled' => 1,
                'visible_on_front' => 1,
                'is_html_allowed_on_front' => 1,
                'user_defined' => true,
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                'group' => 'General Information',
            ]
        );
        $idg = $categorySetup->getAttributeGroupId($entityTypeId, $attributeSetId, 'General Information');
        $categorySetup->addAttributeToGroup(
            $entityTypeId,
            $attributeSetId,
            $idg,
            'bottom_description',
            46
        );

        //$installer->updateAttribute($entityTypeId, 'bottom_description', 'is_wysiwyg_enabled', 1);
        //$installer->updateAttribute($entityTypeId, 'bottom_description', 'is_html_allowed_on_front', 1);

        $installer->endSetup();
    }

Read more here.

  • you also need to add the field in the form using the ui component. – Marius Jun 7 '16 at 13:47
  • @Vasilii - from the comments of your link it says that this code does not work after Mage 2.0.2. We are currently at 2.0.4 and 2.0.7. – Max Jun 7 '16 at 13:53
  • @Marius: could you please elaborate? – Max Jun 7 '16 at 13:54
  • You can see here how the fields are added in the category add/edit form. You need to create a file called view/adminhtml/ui_component/category_form.xml inside your module where you add your new field using the similar markup as in the file I just linked. – Marius Jun 7 '16 at 13:56
  • Do I need the common plugin files in my module folder, like registration.php and stuff? – Max Jun 7 '16 at 14:15

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.