I'm retrieving from the Magento API an order by its increment_id attribute like this:


I have added a plugin to add a custom attribute to the Order items like this (etc/di.xml):

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <type name="Magento\Sales\Api\OrderRepositoryInterface">
        <plugin name="order_custom_extension_attribute"

And then adding the custom attribute (is_gift_card) to the OrderItem like this (etc/extension_attributes.xml)

<?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\OrderItemInterface">
        <attribute code="is_gift_card" type="boolean"/>

Finally my plugin to add the extension attribute to the order items in the afterGet and afterGetList:

    public function __construct(
         \Magento\Sales\Api\Data\OrderExtensionFactory $extensionFactory
    ) {
        $this->extensionFactory = $extensionFactory;

    public function afterGet(\Magento\Sales\Api\OrderRepositoryInterface $subject, \Magento\Sales\Api\Data\OrderInterface $order)
        return $this->addExtensionAttributes($order);

    public function afterGetList(\Magento\Sales\Api\OrderRepositoryInterface $subject, \Magento\Sales\Api\Data\OrderSearchResultInterface $searchResult)
        $orders = $searchResult->getItems();

        foreach ($orders as &$order) {
           $order = $this->addExtensionAttributes($order);

        return $searchResult;

    private function addExtensionAttributes($order){

        $items = $order->getAllItems();
        foreach ($items as $item){
            $giftCard = $item->getData('is_gift_card');
            $extensionAttributes = $item->getExtensionAttributes();
            $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();

        return $order;

The thing is that is_gift_card is added to the extension_attribute segment IF that attribute has any value, but it is not showing at all if it has a null value.

Is this a normal behaviour from the Magento API?

Thanks in advanced

  • Can you please add the this line $giftCard = isset($item->getData('is_gift_card')) ? $item->getData('is_gift_card') : ""; Commented Jul 1, 2020 at 5:49

1 Answer 1


In my opinion, it is totally fine that we don't send null parameters in our API, especially if it is an extension attribute. Why should we send informations that the parameter is empty, if it is optional? By default, if it wasn't passed, we can assume that it isn't present.

I would also use a custom attribute for this and would avoid using extension attributes because they are intended to use more complex data types than booleans.

  • So is this a default behaviour from M2 API? Commented Jul 2, 2020 at 1:22
  • 1
    @PalomaMartinez yes, it's a default behaviour. Commented Jul 2, 2020 at 8:51

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.