0
votes

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>


<?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

I have try to debug this issue but issue has not solved, please help me for figure out issue.

reference from this example : https://magento.stackexchange.com/questions/194705/magento-2-add-extra-text-field-in-checkout-billing-address-and-save-it-in-orde

Thanks.

1
Please share the code of all steps you followed from the reference link. So, it would be easy for us to figure out the issue. - Dhara Bhatti

1 Answers

0
votes

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>