The following steps show you how to find the stock item list in Magento 2:
- Receiver stock item from resources model
- find resource model of stock item
- create the select query
Build and deliver a seamless shopping experience to your customers with Magento today!!!
How to find a stock item list in Magento 2?
In Magento 2, you can get the stock item from the collection by using the following command,
$stockItems = Mage::getResourceModel('cataloginventory/stock_item_collection') ->setPageSize(10) ->setCurPage(1) ->load();
- This command gets the first 10 stock item records from the database, it’s very simple.
- Though you cannot use this solution in Magento 2 try to take a glance at the collection resources of Stock item
/vendor/magento/module-catalog-inventory/Model/ResourceModel/Stock/Item/Collection.php
/** * Class Collection * @package Magento\CatalogInventory\Model\ResourceModel\Stock\Item */ class Collection extends AbstractSearchResult implements StockItemCollectionInterface { ...
- You can see that the collection of a stock item does not extend from the Abstract collection. Hence, we cannot use the function of collection to get data such as set page size(), load().
How can we get the stock item list in Magento?
- I have a simple answer that is receiving a stock item from the resources model
<span style="font-weight: 400;"> </span> $resource = $objectManager->create('Magento\CatalogInventory\Model\ResourceModel\Stock\Item'); $select = $resource->getConnection()->select()->from($resource->getMainTable()); $stockItems = $resource->getConnection()->fetchAll($select);
- First, I get the Resource Model of Stock Item by using $objectManager. Then I create the select query using the connection item. At last, I obtain data from the database using the $select query.
I hope the above-mentioned steps help you find the stock item list in Magento.
Leave a Reply