Geting the available quantity for all products via REST API
문제
How can I get the available quantity for all products via REST API
?. Do I have to create a new module for this? What is the most efficient way to achieve this?
해결책
To get any information about product inventory you must use API from CatalogInventory module. Right now there is not REST API for your case, but there is Service API.
All you need is to use in proper way next method:
\Magento\CatalogInventory\Api\StockItemRepositoryInterface::getList(\Magento\CatalogInventory\Api\StockItemCriteriaInterface $criteria)
You definitely need to write some code.
Create new REST API route in your module. app/code/Your/Module/etc/webapi.xml
<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route url="/V1/stockItems/list/" method="GET">
<service class="Magento\CatalogInventory\Api\StockItemRepositoryInterface" method="getList"/>
<resources>
<resource ref="Magento_CatalogInventory::cataloginventory"/>
</resources>
</route>
</routes>
after that you request will be mapped directly on StockItemRepositoryInterface::getList
.
To test it call /rest/V1/stockItems/list/?criteria=
.
?criteria=
is required
StockItemRepositoryInterface::getList will be executed but request will not return data. Instead you receive something like
{"message":"Internal Error. Details are available in Magento log file. Report ID: webapi-5880a7dd84b8a"}
with message in log file
Report ID: webapi-5880a7dd84b8a; Message: Notice: Undefined index: limit in ....
it because of bag with \Magento\CatalogInventory\Api\StockItemCriteriaInterface
implementation. It doesn't have limit
in data
.
I fixed it with before
plugin
app/code/Your/Module/etc/webapi_rest/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\CatalogInventory\Api\StockItemRepositoryInterface">
<plugin name="fixCriteria" type="Your\Module\Plugin\Api\StockItemRepositoryInterface" sortOrder="60" />
</type>
</config>
Your/Module/Plugin/Api/StockItemRepositoryInterface.php
<?php
namespace Your\Module\Plugin\Api;
use Magento\CatalogInventory\Api\StockItemRepositoryInterface as StockItemRepository;
class StockItemRepositoryInterface
{
public function beforeGetList(
StockItemRepository $subject,
\Magento\CatalogInventory\Api\StockItemCriteriaInterface $criteria
) {
$criteria->setLimit(0, 1000);
}
}