magento 2 extension attribute value is coming null
-
14-04-2021 - |
Question
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
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
Solution
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.
OTHER TIPS
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