我想在customer_address上添加一个自定义属性。我需要将该值保存在DB中,并以前端形式(新/编辑地址)和后端形式触发该值。
我使用以下InstallData脚本来添加新属性:
$installer = $setup;
$installer->startSetup();
/* @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerSetup->removeAttribute('customer_address', "chamber_of_commerce");
$customerEntity = $customerSetup->getEavConfig()->getEntityType('customer_address'); //customer
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSetup->addAttribute('customer_address', 'chamber_of_commerce', [
'type' => 'varchar',
'label' => 'Chamber of Commerce',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'system' => false,
'sort_order' => 150
]);
$attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'chamber_of_commerce')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms'=>['adminhtml_customer_address','customer_address_edit','customer_register_address']
]);
$attribute->save();
$installer->endSetup();
我使用了'used_in_forms'=>['adminhtml_customer_address','customer_address_edit','customer_register_address']
`
但似乎只有后端形式会自动添加,并且还会保存新值。
我手动添加了一个输入,以便在CustomerAccount的新/编辑地址上的前端显示该属性,但是我无法设法从DB获取值或保存新值。
我想我缺少了一些东西,如果有人可以看一下并给出提示,我将不胜感激。
我正在使用Magento 2.2.4
更新: 1.我使用了Magento \ Customer \ Model \ Address的观察者,以便从MyAccount页面保存用于编辑/新地址的值。
2.1。客户地址接口的extension_attributes
<extension_attributes for="Magento\Customer\Api\Data\AddressInterface">
<attribute code="chamber_of_commerce" type="string" />
</extension_attributes>
2.2。等/字段集:
<fieldset id="sales_convert_quote_address">
<field name="chamber_of_commerce">
<aspect name="to_customer_address" />
<aspect name="to_order_address" />
</field>
</fieldset>
2.3。 beforeUpdateData函数的客户地址插件
<type name="Magento\Customer\Model\Address">
<plugin disabled="false" name="vendor_plugin_quote_model_address" sortOrder="10"
type="Vendor\Module\Plugin\Customer\Model\Address"/>
</type>