1

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!

2

3 Answers 3

2

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;        
    }

}
1
  • Tried this approach but don't see any data in extension attributes in Magento 2.2.x site. Any clues?
    – cnu
    Aug 23, 2020 at 1:35
1

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

5
  • Yes. But I need stock status included in Categories List Products. Aug 30, 2019 at 11:01
  • @KirtiNariya Fairly certain that's not possible out of the box, if you mean getting the Category Structure of Products + Stock Information in a single call
    – MackieeE
    Aug 30, 2019 at 11:04
  • I not need Category Structure. But need stock status included category listing Products. Category Page: localhost/magentosample230/gear/watches.html And need stock status in added with watches products. Aug 30, 2019 at 11:09
  • @KirtiNariya Same thing but different terminology, you're specifying a category and it's relevant products "tagged" under that tree; alibet products queries Product Repository, whereas you're wanting product Inventory to be included.
    – MackieeE
    Aug 30, 2019 at 11:16
  • I have to need "stock_item" included in list of the products as a "extension_attributes". Sep 2, 2019 at 11:01
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.