0

I want to create a module like the default module of Magento: Product Attributes in admin, located in Stores -> Attributes -> Product, but for categories, add custom attributes to categories from an admin module. I know how to create a module for the admin, I created a form, but I don't know how to save all the data and save the new attribute.

I read this post: Magento2 Create eav_attribute for catalog but here the attribute is created inside the Setup.

  • This is not necessarily related to the question. It's just my opinion. A module that manages category attributes should not exist. If you want to add an attribute to a category it means it has logic behind it referred in the code. this means that if you add an attribute using the UI then add logic to it, it will not become portable. Moving your code on a clean database will throw errors because the category attribute does not exist. All attributes (categories, products, customers) should be added via install/upgrade scripts. – Marius Mar 21 '16 at 14:45
1

I didn't try any sample but i think with this code will be work

Magento 2.0.x:

With Magento 2.1.x, please read more info in next to answer.

in your controller:

protected $_evaSetup;

public function __construct(.....,
     \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory
){
      parent::__construct($context);
      .....
      $this->_abc = $eavSetupFactory;    
}
public function execute(){
      $categorySetup = $this->_evaSetup->create(['setup' => $setup]);

    /**
     * Add attributes to the eav/attribute
     */
    $entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Category::ENTITY);
    $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId);
    $categorySetup->addAttribute(
    \Magento\Catalog\Model\Category::ENTITY, 'my_attribute', [
         'type' => 'int',
         'label' => 'My Atrribute ',
         'input' => 'select',
         'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
         'required' => false,
         'sort_order' => 100,
         'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
         'group' => 'General Information',
    ]);
     $idg =  $categorySetup->getAttributeGroupId($entityTypeId, attributeSetId, 'General Information');
     $categorySetup->addAttributeToGroup(
           $entityTypeId,
           $attributeSetId,
           $idg,
          'my_attribute',
          46);
}

http://www.ibnab.com/en/blog/magento-2/magento-2-add-custom-eav-attribute-to-category-or-customer

  • HoangHieu, thanks for your answer but what I want is create a category attribute, in your answer you create an attribute for product, but your answer show me a new path to find what I'm looking for. – darkangelo Mar 21 '16 at 13:54
  • Check my update answer ... :) – HoangHieu Mar 21 '16 at 14:41
1

The above solution is not enough to show the Category attribute in the admin Category Edit page. This will create the category attribute, but we will not see in the admin edit page. You need to add view/adminhtml/ui_component/category_form.xml and add the code for the custom field. More info on the blog: http://mage-simplified.blogspot.com/2016/07/magento-210-category-custom-attribute.html

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="content">
        <field name="sample_module_field">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortOrder" xsi:type="number">110</item>
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="label" xsi:type="string" translate="true">Custom Field</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>
  • Thank for advance :) .. My solution for Magento 2.0.x :) your Addition for Magento 2.1.x – HoangHieu Jul 9 '17 at 14:16

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.