1

I an creating a customer address attribute using this code

<?php

namespace Ansh\CustomAddressAttribute\Setup;

use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetup;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class UpgradeData implements  UpgradeDataInterface
{
    private $customerSetupFactory;

    public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
    {
        $this->customerSetupFactory = $customerSetupFactory;
    }
    public function upgrade(ModuleDataSetupInterface $setup,
                            ModuleContextInterface $context){
        $setup->startSetup();

        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerSetup->addAttribute('customer_address', 'sms', [
            'label' => 'Mobile (Courier text notifications)',
            'input' => 'text',
            'type' => \Magento\Framework\DB\Ddl\Table ::TYPE_TEXT,
            'source' => '',
            'required' => false,
            'position' => 333,
            'visible' => true,
            'system' => false,
            'is_used_in_grid' => false,
            'is_visible_in_grid' => false,
            'is_filterable_in_grid' => false,
            'is_searchable_in_grid' => false,
            'backend' => ''
        ]);

        $attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'sms')
            ->addData(['used_in_forms' => [
                'adminhtml_customer_address',
                'adminhtml_customer',
                'customer_address_edit',
                'customer_register_address',
                'customer_address',
            ]]);
        $attribute->save();

        $setup->endSetup();
    }
}

and its visible in my front end on last

enter image description here

also i added extension attribute using

<?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\Quote\Api\Data\AddressInterface">
        <attribute code="sms" type="string" />
    </extension_attributes>
</config>

I am trying to get extension attribute's value in my plugin

as following

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="sms_address_save_plugin" type="Ansh\CustomAddressAttribute\Plugin\ShippingInformationManagementPlugin" sortOrder="10"/>
    </type>
</config>

but when i am using getSms i am getting null

<?php

namespace Ansh\CustomAddressAttribute\Plugin;


class ShippingInformationManagementPlugin
{
    protected $quoteRepository;

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

    /**
     * @param \Magento\Checkout\Model\ShippingInformationManagement $subject
     * @param $cartId
     * @param \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
     */
    public function beforeSaveAddressInformation(
        \Magento\Checkout\Model\ShippingInformationManagement $subject,
        $cartId,
        \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
    ) {
        $shippingAddress = $addressInformation->getShippingAddress();

        $shippingAddressExtensionAttributes = $shippingAddress->getExtensionAttributes();
        var_dump($shippingAddressExtensionAttributes->getSms());die;
        if ($shippingAddressExtensionAttributes) {
            $sms = $shippingAddressExtensionAttributes->getSms();
            $shippingAddress->setSms($sms);
        }

    }
}

whats wrong i am doing Thanks for help

2 Answers 2

2

Knockout.js code reads and sends extension_attributes in the ajax request.

First, you need to fetch the saved attribute value in the js file.

please check

  1. vendor/magento/module-checkout/view/frontend/web/js/view/shipping.js
  2. vendor/magento/module-checkout/view/frontend/web/js/view/form/element/email.js
  3. vendor/magento/module-checkout/view/frontend/web/js/action/select-shipping-address.js

for your reference

from there via ajax request your address data will be passed in the server-side (PHP) code.

otherwise, you will not get the data on the server side.

0

you have created a customer address attribute to save on a quote address entity. I suggest creating an attribute for the quoted address first.

Then, the extension attribute should be on the interface \Magento\Checkout\Api\Data\ShippingInformationExtensionInterface

6
  • Then will I get input in shipping form? May 28, 2020 at 8:53
  • the most important is to change the extension interface in the extension_attributes file.. where you persist the data is possibly not the most important thing to change.. If you give it a go, if it does not work, I am happy to dig more May 28, 2020 at 8:55
  • hi thanks for help but i am not able to find Magento\Checkout\Api\Data\ShippingInformationExtensionInterface in vendor or genrated on what basis are you suggesting May 28, 2020 at 9:11
  • ok, will take a look but that might have to be tomorrow, thanks for your update May 28, 2020 at 9:12
  • thanks for your help May 28, 2020 at 9:14

Your Answer

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

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