Skip to content

Magento 2 How to get the Order Extension_Attribute Value in AfterPlugin

How to get an order’s extension attribute value when creating the order via the API?

For example to create an order I’m calling /rest/V1/carts/mine/payment-information with the following payload:

{
    "payment_method": {
        "method": "cashondelivery"
    },
    "email": "[email protected]",
    "billingAddress": {
        "country_id": "US",
        "street": [
            "N/A"
        ],
        "telephone": "012345678911",
        "postcode": "11501",
        "city": "Bangkok",
        "firstname": "John",
        "lastname": "Key",
        "region": "New York",
        "region_code": "",
        "region_id": 43
    },
    "extension_attributes" : {
       "my_custom_order_attribute": "Custom String"
    }
}

Plugin File: AddCustomOrderAttribute.php – Not getting the value of ExtensionAttribute in the $myCustomOrderAttribute even in the $order->getData()

public function setMyCustomOrderAttributeData(OrderInterface $order)
    {
        /**
         * {@SetLogger}
         */
        $writer = new LaminasLogWriterStream(BP . '/var/log/attr.log');
        $logger = new LaminasLogLogger();
        $logger->addWriter($writer);

        if ($order instanceof MagentoSalesModelOrder) {
        $myCustomOrderAttribute = $order->getExtensionAttributes();
        } else {
            $orderModel = $this->orderFactory->create();
            $orderModel->load($order->getId());
            $myCustomOrderAttribute = $orderModel->getMyCustomOrderAttribute();
        }

        $logger->info(json_encode($order->getData()));

        $extensionAttributes = $order->getExtensionAttributes();
        $orderExtensionAttributes = $extensionAttributes ? $extensionAttributes
            : $this->orderExtensionFactory->create();

        $orderExtensionAttributes->setMyCustomOrderAttribute($myCustomOrderAttribute);
        $order->setExtensionAttributes($orderExtensionAttributes);
    }
    
    /**
     * Add "my_custom_order_attribute" extension attribute to order data object
     * to make it accessible in API data
     *
     * @param OrderRepositoryInterface $subject
     * @param OrderSearchResultInterface $searchResult
     *
     * @return OrderSearchResultInterface
     */
    public function afterGetList(
        OrderRepositoryInterface $subject,
        OrderSearchResultInterface $orderSearchResult
    ) {
        foreach ($orderSearchResult->getItems() as $order) {
            $this->setMyCustomOrderAttributeData($order);
        }
        return $orderSearchResult;
    }

    /**
     * Add "my_custom_order_attribute" extension attribute to order data object
     * to make it accessible in API data
     *
     * @param OrderRepositoryInterface $subject
     * @param OrderInterface $order
     *
     * @return OrderInterface
     */
    public function afterGet(
        OrderRepositoryInterface $subject,
        OrderInterface $resultOrder
    ) {
        $this->setMyCustomOrderAttributeData($resultOrder);
        return $resultOrder;
    }

setAttribute is working fine but getting attribute value from API in this code is not working. Please help me to fix this issue.

Thanks in Advance!!