【问题标题】:Magento2 Add Product Attribute Not WorkingMagento2 添加产品属性不起作用
【发布时间】:2018-09-24 20:02:34
【问题描述】:

我创建了一个文件setup/installdata.php:

    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;

        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\Product::ENTITY,
                'name',
                [
                    'type' => 'int',
                    'backend' => '',
                    'frontend' => '',
                    'label' => 'attrlabel',
                    'input' => 'select',
                    'class' => '',
                    'source' => 'a\b\Model\Attribute\Source\m',
                    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                    'visible' => true,
                    'required' => false,
                    'user_defined' => true,
                    'default' => '',
                    'searchable' => false,
                    'filterable' => false,
                    'comparable' => false,
                    'visible_on_front' => false,
                    'used_in_product_listing' => true,
                    'unique' => false,
                    'apply_to' => '',
                    'attribute_set' => 'Default',
                ]
            );
        }
    }

然后,在 etc/module.xml 中,我有:

    <module name="Conlabz_IdentityCheck" setup_version="1.0.0" />

但我仍然没有看到创建的属性。

我还尝试了以下方法:

在 Magento 根文件夹中,我运行以下命令:

    php bin/magento module:disable
    php bin/magento module:enable
    php bin/magento setup:upgrade
    php bin/magento cache:flush

全部成功,没有错误,但正如我所说,它不起作用。

我做错了什么?

【问题讨论】:

    标签: attributes product magento2


    【解决方案1】:

    检查您的文件名setup/installData.php。

    “D”应该是大写。

    然后在您的 magento2 数据库中搜索表“setup_module”并删除“Conlabz_IdentityCheck”条目。

    然后运行以下命令:

    php bin/magento 设置:升级

    php bin/magento setup:static-content:deploy -f

    【讨论】:

      【解决方案2】:
      1. 首先,文件和文件夹名称应该是Setup/InstallData.php
      2. 在您的 InstallData.php 文件中,您必须在第一行添加以下代码:

        命名空间 Conlabz\IdentityCheck\Setup;

      然后只需运行 CLI 命令。

      但是,如果您仍然遇到问题,那么作为参考,您可以在 Magento Attributes 上查看本指南。

      【讨论】:

        猜你喜欢
        • 2016-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-29
        • 2018-05-04
        • 1970-01-01
        • 1970-01-01
        • 2018-07-04
        相关资源
        最近更新 更多