【问题标题】:Magento 2 - add a category attributeMagento 2 - 添加类别属性
【发布时间】:2019-01-14 17:44:52
【问题描述】:

我正在尝试使用此 guide 创建自定义类别属性,但它对我不起作用,这是我用于 安装数据.php:

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\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, 'disposizione', [
        'type'     => 'int',
        'label'    => 'Disposizione',
        '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',
    ]);
  }
}

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>

模块.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
\Magento\Framework\Component\ComponentRegistrar::register(
  \Magento\Framework\Component\ComponentRegistrar::MODULE,
  'Ng_AddAttribute',
  __DIR__
);

之后,我使用bin/magento setup:upgrade 升级架构并清除缓存,但没有任何反应,也没有错误。 这只是一个尝试,我需要创建两个属性一个文本和一个选择。 有人可以帮我吗

【问题讨论】:

  • 您是否在eav_attribute 表中签入attribute_id = disposizione 存在的一行?如果否,则意味着您的安装脚本将不会运行。在此处添加整个模块代码以分析问题所在。
  • eav_attribute 表中没有新行。这是module.xml的代码&lt;?xml version="1.0"?&gt; &lt;config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"&gt; &lt;module name="Ng_AddAttribute" setup_version="1.0.0" /&gt; &lt;/config&gt;
  • 嘿,我的意思是 Ng_AddAttribute 的所有模块文件
  • 我已经编辑了原帖

标签: magento magento2 magento2.1


【解决方案1】:

您的 InstallData.php 文件中缺少命名空间。用下面的代码替换文件内容或添加命名空间。

<?php
namespace Ng\AddAttribute\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\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, 'disposizione', [
            'type'     => 'int',
            'label'    => 'Disposizione',
            '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',
        ]);
    }
}

要让它再次运行,您需要使用如下查询从 setup_module 表中删除该模块的行:

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

之后运行 setup:upgrade 命令。

【讨论】:

    【解决方案2】:

    文件: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/Modulename\Setup\Patch\Data;
    
        use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
        use Magento\Eav\Setup\EavSetup;
        use Magento\Eav\Setup\EavSetupFactory;
        use Magento\Framework\Setup\ModuleDataSetupInterface;
        use Magento\Framework\Setup\Patch\DataPatchInterface;
        use Magento\Framework\Setup\Patch\PatchRevertableInterface;
    
        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(
                    \Magento\Catalog\Model\Category::ENTITY,
                    'multi_brands',
                    [
                        'type' => 'int',
                        'label' => 'Multi Brands',
                        'input' => 'select',
                        'sort_order' => 333,
                        'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                        '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(\Magento\Catalog\Model\Category::ENTITY, 'multi_brands');
    
                $this->moduleDataSetup->getConnection()->endSetup();
            }
    
            /**
             * {@inheritdoc}
             */
            public function getAliases()
            {
                return [];
            }
    
            /**
             * {@inheritdoc}
             */
            public static function getDependencies()
            {
                return [
    
                ];
            }
        }
    
    
    

    文件: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>
    

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

    这将解决您的问题,希望对您有所帮助

    【讨论】:

      猜你喜欢
      • 2014-05-21
      • 1970-01-01
      • 2016-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多