0

I have a Adobe commerce cloud platform and created one customer address attribute from the admin that is default feature of the Commerce cloud.

Now it works fine. It works well on the customer account creation and checkout page. But i want to get this custom attribute value in the order Object using the REST API.

When i fetch the order data using the REST API, i could not see this custom address attribute. Any idea how to configure?

2 Answers 2

1

For adding the custom attribute in the order Api. You need to create extension attributes and then set that custom attribute value in that.

https://www.atwix.com/magento-2/adding-custom-attribute-to-api-response-in-magento-2/

3
  • I want to add for customer address attribute, So do you have any idea about that? Commented Jun 3, 2024 at 14:58
  • @SunnyRahevar magento.stackexchange.com/questions/88245/… check this for more details for creating the customer custom attribute Commented Jun 3, 2024 at 15:14
  • I am using Adobe features to create the customer address attribute. Commented Jun 3, 2024 at 15:32
1

To include customer address attributes in the Order Data API, Please follow the below steps.

  1. In your module, create an etc/extension_attributes.xml file to declare the extension attribute.
<?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\Sales\Api\Data\OrderInterface"> <attribute code="custom_attribute" type="string" /> </extension_attributes> </config> 
  1. Create a etc/di.xml file to define a plugin.
<?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\Sales\Api\OrderRepositoryInterface"> <plugin name="add_custom_attribute_to_order_data" type="Vendor\Module\Plugin\OrderRepositoryPlugin"/> </type> </config> 
  1. Create a Plugin/OrderRepositoryPlugin.php to get the customer address attribute and set it into order data.

You need to inject the Magento\Customer\Api\AddressRepositoryInterface into your plugin.

<?php namespace Vendor\Module\Plugin; class OrderRepositoryPlugin { protected $addressRepository; public function __construct( \Magento\Customer\Api\AddressRepositoryInterface $addressRepository ) { $this->addressRepository = $addressRepository; } public function afterGet( \Magento\Sales\Api\OrderRepositoryInterface $subject, \Magento\Sales\Api\Data\OrderInterface $order ) { $billingAddressId = $order->getBillingAddressId(); $address = $this->addressRepository->getById($billingAddressId); $customAttribute = $address->getCustomAttribute('custom_attribute'); if ($customAttribute) { $order->setCustomAttribute($customAttribute->getValue()); } return $order; } public function afterGetList( \Magento\Sales\Api\OrderRepositoryInterface $subject, \Magento\Sales\Api\Data\OrderSearchResultInterface $searchResult ) { foreach ($searchResult->getItems() as $order) { $this->afterGet($subject, $order); } return $searchResult; } } 

We can get the billing address ID from the order, using the address repository to load the address, and then get the custom attribute from the address and we can set it to the order data.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.