2

Is there a way to add a custom attribute to a category?

I only see these options in the swagger documentation:

  • GET /V1/categories/attributes/:attributeCode
  • GET /V1/categories/attributes
  • GET /V1/categories/attributes/:attributeCode/options

I don't see any way to create an attribute or attribute group for the category.

Thank you

3 Answers 3

1
$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, 'my_attribute' );

$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',
        ]
    );

$installer->endSetup();

For More information, Please follow this link

0

Create a custom module and add below code to InstallData.php

at app\code\Vendor\ModuleName\Setup\InstallData.php

namespace Vendor\ModuleName\Setup;

use Magento\Eav\Setup\EavSetupFactory;\
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    public function __construct(
        EavSetupFactory $eavSetupFactory
    ) {

        $this->eavSetupFactory = $eavSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
                    \Magento\Catalog\Model\Category::ENTITY,
                    'category_marketplace',
                    [
                        'type' => 'int',
                        'label' => 'Test',
                        'input' => 'select',
                        'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                        'required' => false,
                        'visible' => true,
                        'user_defined' => false,
                        'sort_order' => 100,
                        'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                        'group' => 'General Information',
                    ]
                );
    }
}
0

Add below code inside app/code/Vendor/Categoryattr/Setup/InstallData.php file,

<?php
namespace Vendor\CategoryAttribute\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;


class InstallData implements InstallDataInterface
{
    private $eavSetupFactory; 
    /**
     * Init
     *
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        /**
         * Add attributes to the eav/attribute
         */ 
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Category::ENTITY,
            'custom_attribute',
                      [
                        'type' => 'varchar',
                        'label' => 'Custom Attribute Description',
                        'input' => 'textarea',
                        'required' => false,
                        'sort_order' => 100,
                        'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                        'group' => 'General Information',
                        'is_used_in_grid' => true,
                        'is_visible_in_grid' => false,
                        'is_filterable_in_grid' => true,
            ]
        );


    }
}

remove var folder and run command php bin/magento setup:upgrade to working inside category.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.