I have try to add custom field(textbox) in shipping and billing section on checkout page. I have create extension attribute like this :
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="Magento\Customer\Api\Data\AddressInterface">
<attribute code="countrycode" type="string"/>
</extension_attributes>
</config>
and below code is di.xml
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Quote\Model\BillingAddressManagement">
<plugin disabled="false" name="Checkoutattribute_Plugin_Magento_Quote_Model_BillingAddressManagement" sortOrder="10" type="vendor\extension\Plugin\Magento\Quote\Model\BillingAddressManagement"/>
</type>
<type name="Magento\Quote\Model\ShippingAddressManagement">
<plugin disabled="false" name="Checkoutattribute_Plugin_Magento_Quote_Model_ShippingAddressManagement" sortOrder="10" type="vendor\extension\Plugin\Magento\Quote\Model\ShippingAddressManagement"/>
</type>
</config>
plugin target file
<?php
namespace vendor\extension\Plugin\Magento\Quote\Model;
class ShippingAddressManagement
{
protected $logger;
public function __construct(
\Psr\Log\LoggerInterface $logger
) {
$this->logger = $logger;
}
public function beforeAssign(
\Magento\Quote\Model\ShippingAddressManagement $subject,
$cartId,
\Magento\Quote\Api\Data\AddressInterface $address
) {
$extAttributes = $address->getExtensionAttributes();
if (!empty($extAttributes)) {
try {
$address->setCountrycode($extAttributes->getCountrycode());
} catch (\Exception $e) {
$this->logger->critical($e->getMessage());
}
}
}
}
But its showing error like this
Fatal error: Uncaught Error: Call to undefined method Magento\Quote\Api\Data\AddressExtension::getCountrycode() in app/code/vendor/extension/Plugin/Magento/Quote/Model/ShippingAddressManagement.php:30
Once remove generated folder and try to place order
its showing error like {"message":"Property "Countrycode" does not have accessor method "getCountrycode" in class "Magento\Quote\Api\Data\AddressExtensionInterface".","trace":"#0 /vendor/magento/framework/Reflection/NameFinder.php(59):
Ref from : Magento 2 : Add extra text field in checkout billing address and save it in order
Thanks.