[已解决]自定义custom_address属性无法保存到客户地址簿

4.38K 浏览M2交流区custom attributes

[已解决]自定义custom_address属性无法保存到客户地址簿

給custom_address新增了五个属性,并且在checkout界面显示了, 现在这五个属性可以保存到quote_address和sale_order_address, 但是这五个自定义属性无法保存到addres_book, 如果从后台编辑客户地址,可以保存,但是从checkout页面传值过去,无法保存

extension_attributes.xml

<extension_attributes for="MagentoQuoteApiDataAddressInterface">    
    <attribute code="tax_code" type="string"/>    
    <attribute code="company_type" type="string"/>    
    <attribute code="code_destination" type="string"/>    
    <attribute code="pec" type="string"/>    
    <attribute code="need_invoice" type="string"/>
</extension_attributes>

di.xml

<type name="MagentoCheckoutModelPaymentInformationManagement">    
    <plugin disabled="false"            
           name="Alefix_FiscalAttributes_Plugin_Magento_Checkout_Model_PaymentInformationManagement" sortOrder="10"            
           type="AlefixFiscalAttributesPluginMagentoCheckoutModelPaymentInformationManagement" />
</type>

app/code/Alefix/FiscalAttributes/Helper/Data.php

public function transportFieldsFromExtensionAttributesToObject(
        $fromObject,
        $toObject,
        $extAttributes
    )
    {
        foreach ($extAttributes as $extAttribute) {
            $set = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $extAttribute)));
            $get = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $extAttribute)));
            $value = $fromObject->$get();
            try {
                $toObject->$set($value);
            } catch (Exception $e) {
                $this->logger->critical($e->getMessage());
            }
        }
        return $toObject;
    }

app/code/Alefix/FiscalAttributes/Plugin/Magento/Checkout/Model/PaymentInformationManagement.php

public function beforeSavePaymentInformation(
        MagentoCheckoutModelPaymentInformationManagement $subject,
        $cartId,
        PaymentInterface $paymentMethod,
        AddressInterface $address
    )
    {
        $extAttributes = $address->getExtensionAttributes();
        if (!empty($extAttributes)) {
            try {
                $this->helper->transportFieldsFromExtensionAttributesToObject(
                    $extAttributes,
                    $address,
                    ['company_type', 'need_invoice', 'tax_code', 'pec', 'code_destination']
                );
            } catch (Exception $e) {
                $this->logger->critical($e->getMessage());
            }
        }
        return [$cartId, $paymentMethod, $address];
    }

这是前台的传值

{
    "cartId": "14",
    "billingAddress": {
        "countryId": "IT",
        "region": "Test",
        "street": [
            "3439 Lawman Avenue"
        ],
        "company": "Test",
        "telephone": "703-254-7762",
        "postcode": "20011",
        "city": "Washington",
        "firstname": "Vanleer",
        "lastname": "Carol",
        "vatId": "0123456789",
        "saveInAddressBook": 1,
        "customAttributes": [
            {
                "attribute_code": "code_destination",
                "value": "0123xxdf"
            },
            {
                "attribute_code": "pec",
                "value": "test@example.com"
            },
            {
                "attribute_code": "tax_code",
                "value": "01234567890"
            },
            {
                "attribute_code": "company_type",
                "value": "0"
            },
            {
                "attribute_code": "need_invoice",
                "value": "1"
            }
        ],
        "extension_attributes": {
            "code_destination": "0123xxdf",
            "pec": "test@example.com",
            "tax_code": "01234567890",
            "company_type": "0",
            "need_invoice": "1"
        }
    },
    "paymentMethod": {
        "method": "banktransfer",
        "po_number": null,
        "additional_data": null
    }
}
编辑问题
1

已解决,利用sales_model_service_quote_submit_before 事件进行手动保存

$order = $observer->getEvent()->getData('order');
$billingAddress = $order->getBillingAddress();
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$addressRepository = $objectManager->create('MagentoCustomerApiAddressRepositoryInterface');
$addressObject = $addressRepository->getById($billingAddress->getCustomerAddressId());
$addressObject->setCustomAttribute('company_type', $billingAddress->getCompanyType());
$addressObject->setCustomAttribute('need_invoice', $billingAddress->getNeedInvoice());
$addressObject->setCustomAttribute('tax_code', $billingAddress->getTaxCode());
$addressObject->setCustomAttribute('pec', $billingAddress->getPec());
$addressObject->setCustomAttribute('code_destination', $billingAddress->getCodeDestination());
$addressRepository->save($addressObject);

0