1

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.

1
  • Did you clear /generated folder after your changes? Extension attributes classes are autogenerated, so maybe you are still having the old one. May 26, 2021 at 8:19

1 Answer 1

1

The issue is extension_attribute.xml file

 <extension_attributes for="Magento\Customer\Api\Data\AddressInterface">
            <attribute code="countrycode" type="string"/>
        </extension_attributes>

above code should turn into

 <extension_attributes for="Magento\Quote\Api\Data\AddressInterface">
            <attribute code="countrycode" type="string"/>
        </extension_attributes>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.