Here are the steps that you will follow in this How to get stock items list in Magento 2:
- Receiver stock items from Resource Model
- Get Resource Model of StockItem
- Build the select query
How to get stock items list in Magento 2
- In Magento 1, you can get stock items from collection by using command below
$stockItems = Mage::getResourceModel('cataloginventory/stock_item_collection')
- This command gets first 10 stock item records from database. It’s very easy.
- However you can not use this solution on Magento 2. Try to take a look on the collection resource of Stock Item
* Class Collection
* @package Magento\CatalogInventory\Model\ResourceModel\Stock\Item
class Collection extends AbstractSearchResult implements StockItemCollectionInterface
- You can see that the collection of StockItem doesn’t extends from AbstractCollection. Therefore we can’t use standard functions of collection to get data such as: setPageSize(), load().
- So how can we get list of stock items in Magento2?
- I have an easy solution that is receiving stock items from Resource Model.
- The first, I get Resource Model of StockItem by using $objectManager. Then I build the select query using connection object. Finally, I fetch data from database using $select query.
- Then after this i can get stock items list in Magento 2. Do I need to define this? Or just use the standard stockitem object and pass into an array? In any case, bit confused and need assistance structuring things properly.
- That’s it. Adhere to every one of the introductions previously mentioned and you may complete the How to get stock items list in Magento 2.Hope my post can be helpful, thanks for reading.
We also contribute great content of Magento Download for FREE (all versions)
DON'T MISS THE CHANCE!