skip to Main Content

I’m tryng to create a custom category attribute using this guide but it doesn’t work for me, this is the code that I’m using for the
InstallData.php:

use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoFrameworkSetupInstallDataInterface;

use MagentoEavSetupEavSetup;
use MagentoEavSetupEavSetupFactory;

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(MagentoCatalogModelCategory::ENTITY, 'disposizione', [
        'type'     => 'int',
        'label'    => 'Disposizione',
        'input'    => 'boolean',
        'source'   => 'MagentoEavModelEntityAttributeSourceBoolean',
        'visible'  => true,
        'default'  => '0',
        'required' => false,
        'global'   => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_STORE,
        'group'    => 'Display Settings',
    ]);
  }
}

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="disposizione">
        <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">Nome Produttore</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>
</form>

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="Ng_AddAttribute" setup_version="1.0.0" />
</config>

registration.php

<?php
MagentoFrameworkComponentComponentRegistrar::register(
  MagentoFrameworkComponentComponentRegistrar::MODULE,
  'Ng_AddAttribute',
  __DIR__
);

Schermata_2018-08-07_alle_18.48.29.png

After that i upgrade the schema with bin/magento setup:upgrade and clear the cache, but nothing happens, neither a error.
this is only a try, I need to create two attribute one text and one select.
could someone help me

2

Answers


  1. Namespace is missing in your InstallData.php file. Replace file content with below code or just add namespace.

    <?php
    namespace NgAddAttributeSetup;
    
    use MagentoFrameworkSetupModuleContextInterface;
    use MagentoFrameworkSetupModuleDataSetupInterface;
    use MagentoFrameworkSetupInstallDataInterface;
    
    use MagentoEavSetupEavSetup;
    use MagentoEavSetupEavSetupFactory;
    
    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(MagentoCatalogModelCategory::ENTITY, 'disposizione', [
                'type'     => 'int',
                'label'    => 'Disposizione',
                'input'    => 'boolean',
                'source'   => 'MagentoEavModelEntityAttributeSourceBoolean',
                'visible'  => true,
                'default'  => '0',
                'required' => false,
                'global'   => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_STORE,
                'group'    => 'Display Settings',
            ]);
        }
    }
    

    To have it run again, you need to remove that module’s row from the setup_module table with a query like below:

    DELETE FROM `setup_module` WHERE `module` LIKE ('%Ng_AddAttribute%');
    

    After that run setup:upgrade command.

    Login or Signup to reply.
  2. File : app/code/Namespace/Modulename/Setup/Patch/Data/AddMultiBrandCatAttribute.php

    
        <?php
        /**
         * Copyright © Category All rights reserved.
         * See COPYING.txt for license details.
         */
        declare(strict_types=1);
    
        namespace Namespace/ModulenameSetupPatchData;
    
        use MagentoEavModelEntityAttributeScopedAttributeInterface;
        use MagentoEavSetupEavSetup;
        use MagentoEavSetupEavSetupFactory;
        use MagentoFrameworkSetupModuleDataSetupInterface;
        use MagentoFrameworkSetupPatchDataPatchInterface;
        use MagentoFrameworkSetupPatchPatchRevertableInterface;
    
        class AddMultiBrandCatAttribute implements DataPatchInterface, PatchRevertableInterface
        {
    
            /**
             * @var ModuleDataSetupInterface
             */
            private $moduleDataSetup;
            /**
             * @var EavSetupFactory
             */
            private $eavSetupFactory;
    
            /**
             * Constructor
             *
             * @param ModuleDataSetupInterface $moduleDataSetup
             * @param EavSetupFactory $eavSetupFactory
             */
            public function __construct(
                ModuleDataSetupInterface $moduleDataSetup,
                EavSetupFactory $eavSetupFactory
            ) {
                $this->moduleDataSetup = $moduleDataSetup;
                $this->eavSetupFactory = $eavSetupFactory;
            }
    
            /**
             * {@inheritdoc}
             */
            public function apply()
            {
                $this->moduleDataSetup->getConnection()->startSetup();
                /** @var EavSetup $eavSetup */
                $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
                $eavSetup->addAttribute(
                    MagentoCatalogModelCategory::ENTITY,
                    'multi_brands',
                    [
                        'type' => 'int',
                        'label' => 'Multi Brands',
                        'input' => 'select',
                        'sort_order' => 333,
                        'source' => 'MagentoEavModelEntityAttributeSourceBoolean',
                        'global' => ScopedAttributeInterface::SCOPE_STORE,
                        'visible' => true,
                        'required' => false,
                        'user_defined' => false,
                        'default' => 0,
                        'group' => 'General Information',
                        'backend' => ''
                    ]
                );
    
                $this->moduleDataSetup->getConnection()->endSetup();
            }
    
            public function revert()
            {
                $this->moduleDataSetup->getConnection()->startSetup();
                /** @var EavSetup $eavSetup */
                $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
                $eavSetup->removeAttribute(MagentoCatalogModelCategory::ENTITY, 'multi_brands');
    
                $this->moduleDataSetup->getConnection()->endSetup();
            }
    
            /**
             * {@inheritdoc}
             */
            public function getAliases()
            {
                return [];
            }
    
            /**
             * {@inheritdoc}
             */
            public static function getDependencies()
            {
                return [
    
                ];
            }
        }
    
    
    

    File : view/adminhtml/ui_component/category_form.xml

    
    
    <?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="general">
            <field name="multi_brands">
                <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <item name="sortOrder" xsi:type="number">50</item>
                        <item name="dataType" xsi:type="string">boolean</item>
                        <item name="formElement" xsi:type="string">checkbox</item>
                        <item name="label" xsi:type="string" translate="true">Multi Source</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>
    
    </form>
    

    Run : php -dmemory_limit=6G bin/magento setup:upgrade

    This will solve your problem, Hope that helps

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search