Добавить атрибут клиента Magento2

Я попытался добавить новое поле в 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: обновить, но ничего не произошло.

Кэш по умолчанию отключен.

я также попытался перестроить этот учебник: http://www.extensions.sashas.org/blog/magento-2-make-customer-attribute.html

но это тоже не сработало.

Magento версия 2.1

1 ответ

Я следовал учебнику, http://www.extensions.sashas.org/blog/magento-2-1-3-how-to-make-customer-attribute-update.html добавил нужные мне атрибуты, они показаны в форма, но когда я сохраняю изменения, они не сохраняются в базе данных. Когда я регистрирую информацию непосредственно в базе данных, она отображается в форме, но когда я сохраняю изменения, моя пользовательская информация удаляется.

введите описание изображения здесь

Другие вопросы по тегам