我试图在 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 版