0

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!

0

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

  • Yes. But I need stock status included in Categories List Products. – Kirti Nariya Aug 30 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 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. – Kirti Nariya Aug 30 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 at 11:16
  • I have to need "stock_item" included in list of the products as a "extension_attributes". – Kirti Nariya Sep 2 at 11:01
0

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

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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