Categories
CMS Magento Mastering Development

Magento 2 API: Extension Attribute with null value not showing

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

http://mystore.com/rest/V1/orders?searchCriteria[filter_groups][0][filters][0][field]=increment_id&searchCriteria[filter_groups][0][filters][0][value]=1200040527&searchCriteria[filter_groups][0][filters][0][condition_type]=eq

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"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Sales\Api\OrderRepositoryInterface">
        <plugin name="order_custom_extension_attribute"
                type="MyVendor\Sales\Plugin\OrderRepositoryPlugin"/>
    </type>
</config>

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"/>
    </extension_attributes>
</config>

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();
            $extensionAttributes->setIsGiftCard($giftCard);
            $item->setExtensionAttributes($extensionAttributes);
        }

        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

Leave a Reply

Your email address will not be published. Required fields are marked *