1 回答
TA贡献1810条经验 获得超4个赞
它正在工作,我根据我的要求使用相同的方法使用 InstallData/UpgradeData.php 。
请检查我的答案这会将数据库中的数据保存在表 customer_entity_varchar 中,将属性保存在 eav_attribute 中。检查代码:
<?php
namespace CustomB2BRFQ\Module\Setup;
use Magento\Customer\Model\Customer;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
class UpgradeData implements \Magento\Framework\Setup\UpgradeDataInterface
{
private $eavSetupFactory;
private $eavConfig;
private $attributeResource;
private $customerSetupFactory;
/**
* @var AttributeSetFactory
*/
protected $attributeSetFactory;
protected $moduleDataSetup;
public function __construct(
\Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,
\Magento\Eav\Model\Config $eavConfig,
\Magento\Customer\Model\ResourceModel\Attribute $attributeResource,
CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory,
ModuleDataSetupInterface $moduleDataSetup
) {
$this->eavSetupFactory = $eavSetupFactory;
$this->eavConfig = $eavConfig;
$this->attributeResource = $attributeResource;
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
$this->moduleDataSetup = $moduleDataSetup;
}
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
//$customerSetup->removeAttribute(Customer::ENTITY, "phonenumber");
$customerEntity = $customerSetup->getEavConfig()->getEntityType(Customer::ENTITY);
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
/** attribute_1 */
$customerSetup->addAttribute(
Customer::ENTITY,
'phonenumber',
[
'type' => 'varchar',
'label' => 'Phone Number',
'input' => 'text',
'required' => true,
'visible' => true,
'user_defined' => true,
'sort_order' => 991,
'position' => 991,
'system' => 0,
]
);
$attribute = $customerSetup->getEavConfig()->getAttribute(
Customer::ENTITY,
'phonenumber'
);
$attribute->addData(
[
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer',
'customer_account_create',
'customer_account_edit']
]
);
$attribute->save();
/** attribute_2 */
$customerSetup->addAttribute(
Customer::ENTITY,
'gstnumber',
[
'type' => 'varchar',
'label' => 'GST Number',
'input' => 'text',
'required' => true,
'visible' => true,
'user_defined' => true,
'sort_order' => 992,
'position' => 992,
'system' => 0,
]
);
$attribute = $customerSetup->getEavConfig()->getAttribute(
Customer::ENTITY,
'gstnumber'
);
$attribute->addData(
[
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer',
'customer_account_create',
'customer_account_edit']
]
);
$attribute->save();
}
}
?>
- 1 回答
- 0 关注
- 104 浏览
添加回答
举报