0

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

1

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

please check vendor/magento/module-checkout/view/frontend/web/js/view/shipping.js, vendor/magento/module-checkout/view/frontend/web/js/view/form/element/email.js and vendor/magento/module-checkout/view/frontend/web/js/action/select-shipping-address.js for 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 to create an attribute for quote address first.

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

  • Then will I get input in shipping form? – Adarsh Shukla May 28 '20 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 – Herve Tribouilloy May 28 '20 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 – Adarsh Shukla May 28 '20 at 9:11
  • ok, will take a look but that might have to be tomorrow, thanks for your update – Herve Tribouilloy May 28 '20 at 9:12
  • thanks for your help – Adarsh Shukla May 28 '20 at 9:14

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.