添加客户属性Magento2

时间:2016-07-27 06:23:54

标签: magento2 adminhtml customer

我试图在magentos 2 customer 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设置:升级但没有发生任何事情。

默认情况下禁用缓存。

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

但它也没有用。

Magento 2.1版

1 个答案:

答案 0 :(得分:0)

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

enter image description here