0

We are working to add custom attribute in catalog category display setting. but it not working in my server

could you please check code and let us now where i made mistakes.

code/Tendersoftware/Categoryattr/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Mod ule/etc/module.xsd">
   <module name="Tendersoftware_Categoryattr" setup_version="1.0.6">

   </module></config>

code\Tendersoftware\Categoryattr\Setup\InstallData.php
namespace Tendersoftware\Categoryattr\Setup;
use Magento\Framework\Setup\{
ModuleContextInterface,
ModuleDataSetupInterface,
InstallDataInterface
};

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;

class InstallData implements InstallDataInterface
{
private $eavSetupFactory;

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, 'proteamcat', [
        'type'     => 'int',
        'label'    => 'Pro team category design',
        'input'    => 'boolean',
        'source'   => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
        'visible'  => true,
        'default'  => '0',
        'required' => false,
        'global'   => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
        'group'    => 'Display Settings',
    ]);
}

}

code\Tendersoftware\Categoryattr\View\adminhtml\ui_component\category_form.xml

<?xml version="1.0"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="display_settings">
    <field name="proteamcat">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="dataType" xsi:type="string">boolean</item>
                <item name="formElement" xsi:type="string">checkbox</item>
                <item name="label" xsi:type="string" translate="true">Pro team category design</item>
                <item name="prefer" xsi:type="string">toggle</item>
                <item name="valueMap" xsi:type="array">
                    <item name="true" xsi:type="string">1</item>
                    <item name="false" xsi:type="string">0</item>
                </item>
                <item name="default" xsi:type="number">0</item>
            </item>
        </argument>
    </field>
</fieldset>

code\Tendersoftware\Categoryattr\registration.php

\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Tendersoftware_Categoryattr',
__DIR__
);

1 Answer 1

0

Try the following sample module code to add custom category attribute.

app/code/Anshu/Custom/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Anshu_Custom',
    __DIR__
);

app/code/Anshu/Custom/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Anshu_Custom" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Catalog" />
        </sequence>
    </module>
</config>

app/code/Anshu/Custom/Setup/InstallData.php

<?php

declare(strict_types=1);

namespace Anshu\Custom\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Catalog\Api\Data\CategoryAttributeInterface;
use Magento\Eav\Model\Config as EavConfig;

class InstallData implements InstallDataInterface
{
    /**
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * @var EavConfig
     */
    private $eavConfig;

    public function __construct(EavSetupFactory $eavSetupFactory, EavConfig $eavConfig)
    {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->eavConfig = $eavConfig;
    }
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        //Adding category attribute
        $catAttributeCode = 'cat_custom';

        $eavSetup->addAttribute(CategoryAttributeInterface::ENTITY_TYPE_CODE, $catAttributeCode, [
            'label' => 'My Custom Category Attribute',
            'user_defined' => 1,
            'unique' => 1,
        ]);

        //Adding this new attribute to default attribute group of default attribute set
        $catSetId = $eavSetup->getDefaultAttributeSetId(CategoryAttributeInterface::ENTITY_TYPE_CODE);
        $catGroupId = $eavSetup->getDefaultAttributeGroupId(CategoryAttributeInterface::ENTITY_TYPE_CODE, $catSetId);
        $eavSetup->addAttributeToSet(CategoryAttributeInterface::ENTITY_TYPE_CODE, $catSetId, $catGroupId, $catAttributeCode);

    }
}

Change the code according to your requirement.

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.