Magento2: Magento 2.1 Rest API getting empty extension_attributes field using search criteria on products endpoint

Created on 27 Feb 2017  路  20Comments  路  Source: magento/magento2



If you try to get a products list from the /products endpoints using search criteria you won't get a non empty extension_attributes.

Preconditions


  1. Magento 2.1 (tested with 2.1.2 and 2.1.5)
  2. PHP 7

Steps to reproduce

  1. Perform a curl to YOUR_MAGENTO_HOST/rest/V1/products?searchCriteria[pageSize]=X&searchCriteria[currentPage]=Y with an authorization header
  2. Check the returned JSON

Expected result

  1. As API reference and Swagger say extension_attributes should be not empty and containing some information like stock status and quantity, like the following part of JSON taken from API reference

"weight": 0,
"extension_attributes": {
"stock_item": {
"item_id": 0,
"product_id": 0,
"stock_id": 0,
"qty": 0,
"is_in_stock": true,
"is_qty_decimal": true,
"show_default_notification_message": true,
"use_config_min_qty": true,
"min_qty": 0,
"use_config_min_sale_qty": 0,
"min_sale_qty": 0,
"use_config_max_sale_qty": true,
"max_sale_qty": 0,
"use_config_backorders": true,
"backorders": 0,
"use_config_notify_stock_qty": true,
"notify_stock_qty": 0,
"use_config_qty_increments": true,
"qty_increments": 0,
"use_config_enable_qty_inc": true,
"enable_qty_increments": true,
"use_config_manage_stock": true,
"manage_stock": true,
"low_stock_date": "string",
"is_decimal_divided": true,
"stock_status_changed_auto": 0,
"extension_attributes": {}
},

Actual result

  1. extension_attributes is always empty like the following part of the output I get:

"weight":54,
"extension_attributes":[],
"product_links":[],
"tier_prices":[],

Anyway if you use the /products/SKU API endpoint you can see that the product extension_attributes field is populated.

FrameworWebapi Confirmed Format is valid Ready for Work Reproduced on 2.1.x Reproduced on 2.2.x Reproduced on 2.3.x bug report

Most helpful comment

Same issue facing with Magento CE 2.2.2.

All 20 comments

Same problem here.
@yellow13 any news about it? Thanks

Hi @lorenzosfarra .
no news about it. I was hoping to have a feedback, but nothing till now

Same problem~~
Magento ver. 2.1.5

Same problem Magento 2.1.7

@yellow13 Hi. Thanks for your feedback. The internal ticket was created MAGETWO-70552

Same problem.

@yellow13, thank you for your report.
We've created internal ticket(s) MAGETWO-70552 to track progress on the issue.

Same issue here, Magento CE 2.2.2.
Any indication when will be this implemented?

Same issue facing with Magento CE 2.2.2.

Any news regarding this issue?

distributed-cd

@arturj79 thank you for joining. Please accept team invitation here and self-assign the issue.

When is this going to be resolved? Looks like 2 people attempted it but gave up.

also waiting for this to be able to query out of stock items via api, if anyone's got an idea for a workaround please let me know

is this related to the fix for the single product query? https://github.com/magento/magento2/issues/6781

hope this gets fixed soon

It has been actually fixed in 2.2-develop branch as well.

https://github.com/magento/magento2/commit/1cb5445345c03f7cea1e2e0aba0d835731683416#diff-ff4893a5628d34910295bae32fbcddfd

https://github.com/magento/magento2/commit/f8968c28cae24769bb6ff9079c78e7526df1df92#diff-ff4893a5628d34910295bae32fbcddfd

To fix this problem I have developed an extension with a plugin which add the stock_item on product list inside extension_attributes:

Replace [your vendor] in all appearances.

[your vendor]/FixStockProductListApi/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    '[your vendor]_FixStockProductListApi',
    __DIR__
);

[your vendor]/FixStockProductListApi/etc/di.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright 漏 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Api\ProductRepositoryInterface">
        <plugin name="joinStockToProductRepositoryList" type="[your vendor]\FixStockProductListApi\Model\Plugin\Product\Repository"/>
    </type>
</config>

[your vendor]/FixStockProductListApi/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="[your vendor]_FixStockProductListApi" setup_version="0.1.0" />
</config>

[your vendor]/FixStockProductListApi/Model/Plugin/Product/Repository.php

<?php

namespace [your vendor]\FixStockProductListApi\Model\Plugin\Product;

use Magento\Catalog\Api\Data\ProductExtensionFactory;

class Repository
{
    /**
     * @var \Magento\CatalogInventory\Api\StockRegistryInterface
     */
    private $stockRegistry;
    private $productExtensionFactory;

    /**
     * @param \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
     */
    public function __construct(
        \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
        ProductExtensionFactory $productExtensionFactory
    ) {
        $this->stockRegistry = $stockRegistry;
        $this->productExtensionFactory = $productExtensionFactory;
    }

    /**
     * Add stock item information to the product's extension attributes
     *
     * @param \Magento\Catalog\Model\Product $product
     * @return \Magento\Catalog\Model\Product
     */
    public function afterLoad(\Magento\Catalog\Model\Product $product)
    {
        $productExtension = $product->getExtensionAttributes();
        $productExtension->setStockItem($this->stockRegistry->getStockItem($product->getId()));
        $product->setExtensionAttributes($productExtension);
        return $product;
    }

    public function afterGetList(
        \Magento\Catalog\Api\ProductRepositoryInterface $subject,
        \Magento\Framework\Api\SearchResults $searchResult
    ){
        /** @var \Magento\Catalog\Api\Data\ProductInterface $product */
        foreach ($searchResult->getItems() as $product) {
            $this->addStockToProduct($product);
        }
        return $searchResult;
    }

    public function afterGet
    (
        \Magento\Catalog\Api\ProductRepositoryInterface $subject,
        \Magento\Catalog\Api\Data\ProductInterface $product
    ) {
        $this->addStockToProduct($product);
        return $product;
    }

    /**
     * @param \Magento\Catalog\Api\Data\ProductInterface $product
     * @return self
     */
    private function addStockToProduct(\Magento\Catalog\Api\Data\ProductInterface $product)
    {
        $extensionAttributes = $product->getExtensionAttributes();
        if (empty($extensionAttributes)) {
            $extensionAttributes = $this->productExtensionFactory->create();
        }
        $stock = $this->stockRegistry->getStockItem($product->getId());

        $extensionAttributes->setStockItem($stock);
        $product->setExtensionAttributes($extensionAttributes);
        return $this;
    }
}
Was this page helpful?
0 / 5 - 0 ratings