0

I have created customer address type custom attribute and it's value store as custom attribute now I want to store this value on quote address column.

I have create observer sales_quote_address_save_before type and then do the coding as per below way.

 <?php
 namespace Vendor\Extension\Observer;
 
 use Magento\Framework\Event\Observer;
 use Magento\Framework\Event\ObserverInterface;
 
 class QuoteAddressBeforeSave implements ObserverInterface
 {
 public function execute(Observer $observer)
 {
 $dataObject = $observer->getEvent()->getDataObject();
 $om = \Magento\Framework\App\ObjectManager::getInstance();
 $cusaddress = $om->get('Magento\Customer\Api\AddressRepositoryInterface');
 $addressId = 15;
 $addressData = $cusaddress->getById($addressId);
 $addressidnew = $dataObject->getData('address_id');
 }
 }
?>

So here, I pass static customer id and get custom attribute value it's working. so now I want to store this custom attribute value on quote address table column.

How to get customer address id dynamic base on selection of address on checkout page and store value on quote address.

asked Sep 22, 2023 at 6:00
1
  • Are you getting selected ```address id` at event sales_quote_address_save_before using $observer->getEvent()->getData('quote_address')->getData('customer_address_id') Commented Sep 22, 2023 at 7:15

1 Answer 1

0

You have to create a field at quote_address using db_scheme.xml at your module app/code/{Vendorname}/{Module}/etc .Assume that your field name is my_field . This field will save the value for your attribute.

<?xml version="1.0" ?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
 <table name="quote_address">
 <column name="my_field" nullable="true" xsi:type="varchar" comment="my_field" length="255"/>
 </table>
</schema>

You can get your selected Customer address ID at sales_quote_address_save_before observer using below code:

$object = $observer->getEvent()->getDataObject();
$addressId = $object->getCustomerAddressId();

Observer code:

<?php
namespace Vendor\Extension\Observer;
use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\LocalizedException;
class QuoteAddressBeforeSave implements ObserverInterface
{
 /**
 * @var AddressRepositoryInterface
 */
 private $addressRepository;
 public function __construct(AddressRepositoryInterface $addressRepository)
 {
 $this->addressRepository = $addressRepository;
 }
 /**
 * @inheritDoc
 */
 public function execute(Observer $observer)
 {
 $object = $observer->getEvent()->getDataObject();
 $addressId = $object->getCustomerAddressId();
 $newField = 'customer_new_field';
 if (!empty($addressId) && (int) $addressId > 0){
 try {
 $customerAddress = $this->addressRepository->getById((int)$addressId);
 if (null !== $customerAddress->getCustomAttribute($newField)){
 $object->setData('my_field',$customerAddress->getCustomAttribute($newField)->getValue());
 }
 } catch (LocalizedException $e) {
 }
 }
 }
}
answered Sep 22, 2023 at 7:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.