1

显示自定义属性“province”。值保存在“customer_address”、“quote_address”和“sales_order_address”表中。

setup > Upgrade.php 保护函数 _addAttributes($setup) {

    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

    $customerSetup->addAttribute('customer_address', 'province', [
        'label' => 'Province',
        'input' => 'text',
        'type' => 'varchar',
        'source' => '',
        'required' => false,
        'position' => 333,
        'visible' => true,
        'system' => false,
        'is_used_in_grid' => false,
        'is_visible_in_grid' => false,
        'is_filterable_in_grid' => false,
        'is_searchable_in_grid' => false,
        'backend' => '',
        'comment' => 'Province'
    ]);


    $attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'province')
        ->addData(['used_in_forms' => [
            'customer_register_address',
            'customer_account_create',
            'adminhtml_customer_address',
            'customer_account_edit',
            'adminhtml_customer',
            'adminhtml_checkout',
            'checkout_register',
        ]]);
    $attribute->save();

    $setup->getConnection()->addColumn(
        $setup->getTable('quote_address'),
        'province',
        [
            'type' => 'text',
            'length' => 255,
            'comment' => 'Province'
        ]
    );

    $setup->getConnection()->addColumn(
        $setup->getTable('sales_order_address'),
        'province',
        [
            'type' => 'text',
            'length' => 255,
            'comment' => 'Province'
        ]
    );

    $setup->endSetup();
}

字段集.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:DataObject/etc/fieldset.xsd">
<scope id="global">
    <fieldset id="customer_address">
        <field name="province">
           <aspect name="to_quote_address" />
        </field>
    </fieldset>
    <fieldset id="sales_convert_quote_address">
        <field name="extension_attributes">
            <aspect name="to_customer_address" />
            <aspect name="to_order_address" />
        </field>
    </fieldset>
    <fieldset id="sales_convert_order_address">
        <field name="extension_attributes">
             <aspect name="to_quote_address" />
        </field>
    </fieldset>
</scope>

创建观察者以在集合中添加属性:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">

<event name="sales_quote_address_save_after">
  <observer name="iversa_customaddress_observer_address" instance="Iversa\CustomAddress\Observer\Address" />
</event>
<event name="sales_order_address_save_after">
    <observer name="iversa_customaddress_observer_address" instance="Iversa\CustomAddress\Observer\Order\Address" shared="false" />
</event>
<event name="sales_quote_address_collection_load_before">
    <observer name="iversa_customaddress_observer_addresscollectionload" instance="Iversa\CustomAddress\Observer\AddressCollectionLoad" shared="false" />
</event>
<event name="sales_order_address_collection_load_before">
    <observer name="iversa_customaddress_observer_order_addresscollectionload" instance="Iversa\CustomAddress\Observer\Order\AddressCollectionLoad" shared="false" />
</event>
</config>

我参考了https://web4pro.net/blog-news/magento-2-add-custom-attribute-customers-address/http://devdocs.magento.com/guides/v2.1/howdoi/checkout/ checkout_new_field.html以显示 magento2 onstepcheckout 地址中的值。另请参阅相同的屏幕截图。

4

0 回答 0