How to Get Custom Order Attributes in the Default Order API Response in Magento 2.3
Introduction
In Magento 2.3, you can easily create custom order attributes and display them in the sales order grid. However, retrieving these custom attributes through the default order API response might require some additional setup. Let’s dive into a common issue faced by developers and find a solution to get your custom order attributes displayed correctly in the API response.
The Problem: Custom Attributes Missing from Order API Response
You’ve created a new custom order attribute like delivery_date or no_of_days. You can see these attributes perfectly displayed in the sales order grid after following the standard Magento setup for adding custom attributes to an entity. However, when you fetch order data using the default order API (e.g., /rest/V1/orders), those custom attributes are missing from the response.
The error message might look something like this:
Fatal error: Uncaught Error: Call to undefined method Magento\Sales\Api\Data\OrderExtension::setTipAndTrickAttribute()
This occurs because by default, Magento doesn’t include your custom attributes in the API response for security and performance reasons. To display these attributes, you need to extend the order data model.
Solving the Issue: Extending Order Data Model
To get your custom order attributes displayed in the API response, you’ll need to follow a two-step process:
- Define your custom attribute in an extension_attributes.xml file
Create an
extension_attributes.xmlfile underAmos/CustomOrder/etc/. In this file, map your custom attribute to the proper type.
<?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="delivery_date" type="string" />
<!-- Add other custom attributes here -->
</extension_attributes>
</config>
- Use an observer to set the extension attribute data
Create an observer class
Amos/CustomOrder/Observer/Sales/OrderLoadAfter.php. This class will be triggered after each order is loaded, allowing you to set your custom data.
namespace Amos\CustomOrder\Observer\Sales;
use Magento\Framework\Event\ObserverInterface;
class OrderLoadAfter implements ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer)
{
$order = $observer->getOrder();
$extensionAttributes = $order->getExtensionAttributes();
if ($extensionAttributes === null) {
$extensionAttributes = $this->getOrderExtensionDependency();
}
$attr = $order->getData('delivery_date');
$extensionAttributes->setTipAndTrickAttribute($attr);
$order->setExtensionAttributes($extensionAttributes);
}
private function getOrderExtensionDependency()
{
return \Magento\Framework\App\ObjectManager::getInstance()->get(
'\Magento\Sales\Api\Data\OrderExtension'
);
}
}
Don’t forget to declare this observer in your events.xml file:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_order_load_after">
<observer name="sales_order_load_delivery_date" instance="Magestore\TipAndTrick\Observer\Sales\OrderLoadAfter" />
</event>
</config>
Conclusion
By following these steps, you should now be able to see your custom order attributes in the default order API response. This approach allows you to control which data is exposed through the API, ensuring that sensitive information remains private.
Remember, this solution assumes you have already added your custom attributes to the sales_order table and displayed them in the sales order grid using techniques like editing di.xml or creating a new column via setup script.
As always, keep an eye on Magento’s official documentation at https://laravelcompany.com for updates and other best practices related to extending Magento functionality.