1

I followed this link for creating the custom address field.

I'm getting this error

PHP Fatal error: Uncaught Error: Call to undefined method Magento\Sales\Api\Data\OrderExtension::setCustomerAddrId()

http://techjeffyu.com/blog/magento-2-add-a-custom-field-to-checkout-shipping

how to resolve this. Please help me.

/var/www/html/horseware_down/app/code/Dev/Testing/etc/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\Checkout\Model\ShippingInformationManagement">
        <plugin name="save_custom_notes_in_quote" type="Dev\Testing\Plugin\Checkout\Model\ShippingInformationManagement" sortOrder="1"/>
    </type>
</config>

events.xml

<?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_model_service_quote_submit_before">
        <observer name="extra_shipping_cost" instance="Dev\Testing\Observer\SaveOrderObserver" />
    </event>
    <event name="sales_model_service_quote_submit_before">
        <observer name="dev_sales_model_service_quote_submit_before" instance="Dev\Testing\Observer\SalesModelServiceQuoteSubmitBefore"/>
    </event>
</config>

extension_attributes.xml

<?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\Checkout\Api\Data\ShippingInformationInterface">
        <attribute code="customer_addr_id" type="string" />
    </extension_attributes>
</config>

frontend/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Checkout\Block\Checkout\LayoutProcessor">
        <plugin name="add_custom_notes_checkout_form" type="Dev\Testing\Plugin\Checkout\Block\Checkout\LayoutProcessor" sortOrder="10"/>
    </type>
</config>

Observer/SalesModelServiceQuoteSubmitBefore.php

<?php
namespace Dev\Testing\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;

class SalesModelServiceQuoteSubmitBefore implements ObserverInterface
{
    /**
     * @param Observer $observer
     * @return $this
     */
    public function execute(Observer $observer)
    {
        /** @var \Magento\Sales\Model\Order $order */
        $order = $observer->getEvent()->getData('order');

        /** @var \Magento\Quote\Model\Quote $quote */
        $quote = $observer->getEvent()->getData('quote');
        $shippingAddressData = $quote->getShippingAddress()->getData();
        if (isset($shippingAddressData['customer_addr_id'])) {
            $order->getShippingAddress()->setCustomerAddrId($shippingAddressData['customer_addr_id']);
        }

        return $this;
    }
}

SaveOrderObserver.php

<?php
namespace Dev\Testing\Observer;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;

class SaveOrderObserver implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $order = $observer->getEvent()->getOrder();
        $quote = $observer->getEvent()->getQuote();

        $order->setData('customer_addr_id', $quote->getCustomerAddrId());

        return $this;
    }
}

app/code/Dev/Testing/Plugin/Checkout/Block/Checkout/LayoutProcessor.php

<?php
namespace Dev\Testing\Observer;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;

class SaveOrderObserver implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $order = $observer->getEvent()->getOrder();
        $quote = $observer->getEvent()->getQuote();

        $order->setData('customer_addr_id', $quote->getCustomerAddrId());

        return $this;
    }
}

app/code/Dev/Testing/Plugin/Checkout/Model/ShippingInformationManagement.php

<?php
namespace Dev\Testing\Plugin\Checkout\Model;


class ShippingInformationManagement
{
    protected $quoteRepository;

    protected $dataHelper;

    public function __construct(
        \Magento\Quote\Model\QuoteRepository $quoteRepository
    )
    {
        $this->quoteRepository = $quoteRepository;
    }

    public function beforeSaveAddressInformation(
        \Magento\Checkout\Model\ShippingInformationManagement $subject,
        $cartId,
        \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
    )
    {
        if(!$extensionAttributes = $addressInformation->getExtensionAttributes())
        {
            return;
        }
        
        $quote = $this->quoteRepository->getActive($cartId);
        $quote->setCustomerAddrId($extensionAttributes->getCustomerAddrId());
    }
}

Dev\Testing\Setup\InstallSchema.php

<?php
namespace Dev\Testing\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;

class InstallSchema implements InstallSchemaInterface
{
    public function install(
        SchemaSetupInterface $setup, 
        ModuleContextInterface $context
    ){
        $setup->startSetup();

        $quote = $setup->getTable('quote');
        $salesOrder = $setup->getTable('sales_order');
        
        
        $setup->getConnection()->addColumn(
            $quote,
            'customer_addr_id',
            [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                'nullable' => true,
                'comment' =>'Customer Addr Id'
            ]
        );
        
        $setup->getConnection()->addColumn(
            $salesOrder,
            'customer_addr_id',
            [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                'nullable' => true,
                'comment' =>'Customer Addr Id'
            ]
        );
        
        $setup->endSetup();
    }
}

app/code/Dev/Testing/view/frontend/web/js/model/shipping-save-processor/default.js

define(
    [
        'jquery',
        'ko',
        'Magento_Checkout/js/model/quote',
        'Magento_Checkout/js/model/resource-url-manager',
        'mage/storage',
        'Magento_Checkout/js/model/payment-service',
        'Magento_Checkout/js/model/payment/method-converter',
        'Magento_Checkout/js/model/error-processor',
        'Magento_Checkout/js/model/full-screen-loader',
        'Magento_Checkout/js/action/select-billing-address'
    ],
    function (
        $,
        ko,
        quote,
        resourceUrlManager,
        storage,
        paymentService,
        methodConverter,
        errorProcessor,
        fullScreenLoader,
        selectBillingAddressAction
    ) {
        'use strict';

        return {
            saveShippingInformation: function () {
                var payload;

                if (!quote.billingAddress()) {
                    selectBillingAddressAction(quote.shippingAddress());
                }
                
                var customer_addr_id = $('[name="custom_attributes[customer_addr_id]"]').val();

                payload = {
                    addressInformation: {
                        shipping_address: quote.shippingAddress(),
                        billing_address: quote.billingAddress(),
                        shipping_method_code: quote.shippingMethod().method_code,
                        shipping_carrier_code: quote.shippingMethod().carrier_code,
                        extension_attributes:{
                            customer_addr_id: customer_addr_id  
                    
                        }
                    }
                };

                fullScreenLoader.startLoader();

                return storage.post(
                    resourceUrlManager.getUrlForSetShippingInformation(quote),
                    JSON.stringify(payload)
                ).done(
                    function (response) {
                        quote.setTotals(response.totals);
                        paymentService.setPaymentMethods(methodConverter(response.payment_methods));
                        fullScreenLoader.stopLoader();
                    }
                ).fail(
                    function (response) {
                        errorProcessor.process(response);
                        fullScreenLoader.stopLoader();
                    }
                );
            }
        };
    }
);

app/code/Dev/Testing/view/frontend/requirejs-config.js

var config = {
    "map": {
        "*": {
            'Magento_Checkout/js/model/shipping-save-processor/default': 'Dev_Testing/js/model/shipping-save-processor/default'
        }
    }
};
1

Try to change extension_attributes.xml :-

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
      <extension_attributes for="Magento\Quote\Api\Data\AddressInterface">
           <attribute code="customer_addr_id" type="string" />
      </extension_attributes>
</config>
0

You need to review your Javascript/HTML Templates to debug why getCustomAttributeLabel is not a function.

Without seeing your code it's difficult to tell you why.

It could be you are trying to access the value as a KO observable when it isn't created as one?

3
  • I followed the same thing as given in this link techjeffyu.com/blog/… – venkata prasad May 20 at 12:26
  • That article doesn't contain getCustomAttributeLabel so it sounds like you have removed or overridden something Magento needs. Without seeing all your code it isn't easy to say what. – Ben Crook May 20 at 14:37
  • added the code. please check. please are not passing into quote and order tables @Ben Crook – venkata prasad May 21 at 11:54
0

you can take a reference from this extension

https://github.com/sivajik34/Custom-Fee-Magento2/tree/master/Sivajik34/CustomFee

2
  • I need for shipping attribute – venkata prasad May 25 at 10:56
  • yes its using same code. please look it once – Gohil Rajesh May 25 at 11:00
0

The issue is extension_attribute.xml file

 <extension_attributes for="Magento\Checkout\Api\Data\ShippingInformationInterface">
        <attribute code="customer_addr_id" type="string" />
    </extension_attributes>

above code should turn into

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

https://codeblog.experius.nl/magento-2-add-extra-billing-or-shipping-field-to-the-checkout/

Better Explanation in their. But there also made a mistake in extension_attribute.xml.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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