Question

I have checked Category Products list API and gives list of the categories products. But don't get stock item details in products details. I want to need stock item details in products details.

Categories list products default API.

Method: GET

Using API URL: http://localhost/magentosample230/rest/V1/products?searchCriteria[pageSize]=10&searchCriteria[filter_groups][0][filters][0][field]=category_id&searchCriteria[filter_groups][0][filters][0][value]=4&searchCriteria[filter_groups][0][filters][0][condition_type]=eq

If any have idea how to get stock item details in Category Products list page using Rest API.

Any help would be appreciated. Thanks!

Was it helpful?

Solution

I have added stock_item in Category List Product details Rest API. Update ExtensionPool data of Magento\Catalog\Api\Data\ProductInterface and add stock_item data provider.

File path: magento\app\code\Vendor\StockitemApi\registration.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

use \Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Vendor_StockitemApi', __DIR__);

File path: magento\app\code\Vendor\StockitemApi\etc\module.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:Module/etc/module.xsd">
    <module name="Vendor_StockitemApi" >
    </module>
</config>

File path: magento\app\code\Vendor\StockitemApi\etc\di.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\EntityManager\Operation\ExtensionPool">
        <arguments>
            <argument name="extensionActions" xsi:type="array">
                <item name="Magento\Catalog\Api\Data\ProductInterface" xsi:type="array">
                    <item name="read" xsi:type="array">
                        <item name="stock_item" xsi:type="string">Vendor\StockitemApi\Model\ReadHandler</item>
                    </item>
                </item>
            </argument>
        </arguments>
    </type>
</config>

File path: magento\app\code\Vendor\StockitemApi\Model\ReadHandler.php

<?php

namespace Vendor\StockitemApi\Model;


class ReadHandler implements \Magento\Framework\EntityManager\Operation\ExtensionInterface
{

    /**
     * @var \Magento\CatalogInventory\Api\StockRegistryInterface
     */
    private $stockRegistry;

    public function __construct(
    \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry     
    ) {

        $this->stockRegistry = $stockRegistry;
    }
    /**
     * Magento\CatalogInventory\Api\Data\StockItemInterface
     * @param type $product
     * @param type $arguments
     */
    public function execute($product, $arguments = [])
    {
        if ($product->getExtensionAttributes()->getStockItem() !== null) {
            return $product;
        }

        $stockItem =$this->stockRegistry->getStockItem($product->getId());
        $extensionAttributes = $product->getExtensionAttributes();
        $extensionAttributes->setStockItem($stockItem);
        $product->setExtensionAttributes($extensionAttributes);

        return $product;        
    }

}

OTHER TIPS

You'll need to use /stockItems/:sku to fetch Stock information. In addition, optionally provide a scopeId to narrow the stock for a given store. So hence in your case:

http://localhost/magentosample230/rest/V1/stockItems/:sku

Using this API

http://localhost/magentosample230/rest/V1/products?searchCriteria[pageSize]=10&searchCriteria[filter_groups][0][filters][0][field]=category_id&searchCriteria[filter_groups][0][filters][0][value]=4&searchCriteria[filter_groups][0][filters][0][condition_type]=eq

I got the stock_item product details but only using admin token.

My Question about how can i get stock_item using customer token?

Using Admin token i am getting result of stock_item succcessfully:

"stock_item": { "item_id": 1420, "product_id": 1417, "stock_id": 1, "qty": 1, "is_in_stock": true, "is_qty_decimal": false, "show_default_notification_message": false, "use_config_min_qty": true, "min_qty": 0, "use_config_min_sale_qty": 1, "min_sale_qty": 1, "use_config_max_sale_qty": true, "max_sale_qty": 10000, "use_config_backorders": true, "backorders": 0, "use_config_notify_stock_qty": true, "notify_stock_qty": 1, "use_config_qty_increments": true, "qty_increments": 0, "use_config_enable_qty_inc": true, "enable_qty_increments": false, "use_config_manage_stock": true, "manage_stock": true, "low_stock_date": null, "is_decimal_divided": false, "stock_status_changed_auto": 0 }, "is_wishlist_product": false },

BUT using customer token stock_item Array not display any values results comes blank.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top