Hi I want to save a shipping address attribute by observer sales_model_service_quote_submit_before
Bellow My observer:
public function execute(Observer $observer)
{
$quote = $observer->getEvent()->getQuote();
$fields = $this->fieldsManagement->getByQuoteId($quote->getId());
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$resource = $objectManager->get('MagentoFrameworkAppResourceConnection');
$customer_res = $objectManager->create('MagentoCustomerApiAddressRepositoryInterface');
$customer = $customer_res->getById(101);
$shippingAddress = $quote->getShippingAddress();
$shippingAddressExtensionAttributes = $shippingAddress->getExtensionAttributes();
if ($shippingAddressExtensionAttributes) {
$customField = $shippingAddressExtensionAttributes->getDistance();
$customer->setCustomAttribute('distance',$customField);
}else{
$customer->setCustomAttribute('distance','not save');
}
}
It Is the observer, I have the
requirejs-config.js
var config = {
config: {
mixins: {
'Magento_Checkout/js/action/set-shipping-information': {
'Danielozano_CustomerAttribute/js/action/set-shipping-information-mixin': true
}
}
}
};
set-shipping-information-mixin.js
define([
'jquery',
'mage/utils/wrapper',
'Magento_Checkout/js/model/quote'
], function ($, wrapper, quote) {
'use strict';
alert('test');
return function (setShippingInformationAction) {
return wrapper.wrap(setShippingInformationAction, function (originalAction) {
var shippingAddress = quote.shippingAddress();
if (shippingAddress['extension_attributes'] === undefined) {
shippingAddress['extension_attributes'] = {};
}
shippingAddress['extension_attributes']['distance'] = 'tested';
//shippingAddress['extension_attributes']['distance'] = jQuery('[custom_attributes[distance]]').val();
return originalAction();
});
};
});
Alos have extension_attributes value to please check bellow image:
https://i.stack.imgur.com/rMwme.png
extension_attributes.xml
< ?xml version="1.0" encoding="UTF-8"?>
fieldset.xml
di.xml
ShippingAddressManagement.php
< ?php
namespace DanielozanoCustomerAttributePluginMagentoQuoteModel;
class ShippingAddressManagement
{
protected $logger;
public function __construct(
PsrLogLoggerInterface $logger
) {
$this->logger = $logger;
}
public function beforeAssign(
MagentoQuoteModelShippingAddressManagement $subject,
$cartId,
MagentoQuoteApiDataAddressInterface $address
) {
//exit;
$extAttributes = $address->getExtensionAttributes();
if (!empty($extAttributes)) {
try {
$address->setDistance($extAttributes->getDistance());
} catch (Exception $e) {
$this->logger->critical($e->getMessage());
}
}
}
}
My issue is when I place order address save but on distance attbiute it should save the distance value test but it show blank. If I use $shippingAddress->getExtensionAttributes() to save as a value then it show 1 on distance field. So 1 is save.