Magento2在结帐中保存新的地址属性

时间:2018-10-03 03:05:47

标签: magento2 magento2.2.3

我正在尝试保存地理位置以及客户地址。

我已经使用安装脚本添加了Let&Lng

    $customerSetup->addAttribute('customer_address', 'latitude', [
        'type' => 'varchar',
        'label' => 'Latitude',
        'input' => 'text',
        'required' => false,
        'visible' => true,
        'visible_on_front' => true,
        'user_defined' => false,
        'sort_order' => 43,
        'position' => 43,
        'system' => 0,
    ]);

    $attributeLat = $customerSetup->getEavConfig()->getAttribute('customer_address', 'latitude')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address','customer_address'],
        ]);
    $attributeLat->save();
    //latitude - End

    $customerSetup->addAttribute('customer_address', 'longitude', [
        'type' => 'varchar',
        'label' => 'Longitude',
        'input' => 'text',
        'required' => false,
        'visible' => true,
        'visible_on_front' => true,
        'user_defined' => false,
        'sort_order' => 43,
        'position' => 43,
        'system' => 0,
    ]);

    $attributeLng = $customerSetup->getEavConfig()->getAttribute('customer_address', 'longitude')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address','customer_address'],
        ]);
    $attributeLng->save();
    //longitude - End}

我可以在管理后端更新值,而不会出现任何问题。

在结帐页面上,将出现“纬度”和“经度”字段。但是,值不会与客户地址一起保存。

我正在使用Magento CE 2.2.3

任何帮助将不胜感激。

谢谢

1 个答案:

答案 0 :(得分:1)

  • 您需要为Magento \ Customer \ Api \ Data \ AddressInterface添加etc / extension_attributes.xml:
<extension_attributes for="Magento\Customer\Api\Data\AddressInterface">
        <attribute code="longitude" type="string" />
    </extension_attributes>
  • 添加etc / fieldset.xml:
 <fieldset id="sales_convert_quote_address">
            <field name="longitude">
                <aspect name="to_customer_address" />
                <aspect name="to_order_address" />
            </field>
        </fieldset>
  • 在di.xml中为Magento \ Customer \ Model \ Address添加插件,您需要将自定义属性保存在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>
  public function beforeUpdateData(
        \Magento\Customer\Model\Address $subject,
        \Magento\Customer\Api\Data\AddressInterface $address
    )

然后您应该可以看到属性保存在客户地址上。

希望有帮助!