0

I'm currently using the following endpoint to retrieve the last 50 orders:

/rest/V1/orders/?searchCriteria[sortOrders][0][field]=created_at&searchCriteria[pageSize]=50

However, I've noticed that the product attribute "searchable_sku" is missing from the response for the products within the orders. How can I modify the API response to include this attribute?

1 Answer 1

0

To include the "searchable_sku" attribute in the order response, you will need to modify the default search criteria and add the attribute to the attribute set that is being used by the API. You can do this by creating a new module in Magento 2 and using a plugin to modify the search criteria.

Here are the steps you can follow:

Create a new module in Magento 2. Create a plugin for the \Magento\Sales\Api\OrderRepositoryInterface interface. In the plugin, modify the search criteria to include the "searchable_sku" attribute. Modify the attribute set used by the API to include the "searchable_sku" attribute. Here is an example of how you can modify the search criteria in the plugin:


use Magento\Sales\Api\OrderRepositoryInterface;

class OrderRepositoryPlugin
{
    public function afterGetList(
        OrderRepositoryInterface $subject,
        \Magento\Sales\Api\Data\OrderSearchResultInterface $searchResult
    ) {
        foreach ($searchResult->getItems() as $order) {
            foreach ($order->getAllItems() as $item) {
                $product = $item->getProduct();
                $searchableSku = $product->getSearchableSku();
                $item->setData('searchable_sku', $searchableSku);
            }
        }

        return $searchResult;
    }
}

In this example, we are using the afterGetList method to modify the search result by adding the "searchable_sku" attribute to each item in the order. You can also modify the method to include other attributes that you need.

Remember to modify the attribute set used by the API to include the "searchable_sku" attribute. You can do this by going to Stores > Attributes > Attribute Set in the Magento admin panel and adding the attribute to the set used by the API.

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.