2

我试图在 magentos 2 客户 adminhtml 中添加一个新字段

我做了什么:

Setup在我的模块中放置一个文件夹InstallData.php

<?php

namespace Company\Module\Setup;

use Magento\Framework\Module\Setup\Migration;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Catalog\Setup\CustomerSetupFactory;
use Magento\Eav\Model\AttributeRepository;

/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{

    /**
     * @var CustomerSetupFactory
     */
    protected $customerSetupFactory;


    protected $attributeRepository;

    /**
     * @param CustomerSetupFactory $customerSetupFactory
     * @param AttributeSetFactory $attributeSetFactory
     */
    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        AttributeRepository $attributeRepository
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeRepository = $attributeRepository;
    }


    /**
     * {@inheritdoc}
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {

        $installer = $setup;
        $installer->startSetup();

        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerSetup->addAttribute(CUSTOMER::ENTITY, 'custom_field', [
            'type' => 'varchar',
            'label' => 'Custom Field',
            'input' => 'text',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 1000,
            'position' => 1000,
            'system' => 0,
        ]);

        $MyAttribute = $customerSetup->getEavConfig()->getAttribute(CUSTOMER::ENTITY, 'custom_field');
        $MyAttribute->setData(
            'used_in_forms',
            ['adminhtml_customer']
        );

        $this->attributeRepository->save($MyAttribute);

        $setup->endSetup();


    }
}

然后我调用了 php bin/magento setup:upgrade 但什么也没发生。

默认情况下禁用缓存。

我还尝试重建本教程:http ://www.extensions.sashas.org/blog/magento-2-make-customer-attribute.html

但它也没有用。

Magento 2.1 版

4

1 回答 1

0

我按照教程 http://www.extensions.sashas.org/blog/magento-2-1-3-how-to-make-customer-attribute-update.html添加了我需要的属性,它们显示在表单,但是当我保存更改时,它们不会保存到数据库中。当我直接在数据库中注册信息时,它会显示在表单中,但是当我保存更改时,我的自定义信息会被删除。

在此处输入图像描述

于 2017-02-09T14:44:25.010 回答