Get / Update Stock Items in Magento 2
This post will introduce a simple way to collect the list of all your items in the inventory when you work with code from the Magento 2 backend.
Inventory management is one of the most important things when it comes to running an online business. As a store owner, you need to manage and keep track of the stock status of your products to avoid selling out-of-stock products or fill the stock on time when something is running out of.
In Magento 2 stores, getting and updating stock items can be done easily in the backend configuration.
In the topic How to get stock items list Magento 2 today, I will introduce a simple way to collect the list of all your items in the inventory when you work with code from Magento 2 backend. If you are having difficulty updating stock items in Magento 2, let’s pass via three steps to complete that.
3 Steps to get stock items list in Magento 2
- Step 1: Collection stock item from
Resource Model
- Step 2: Take
Resource Model of StockItem
- Step 3: Set the select query
Let’s see the details of each step.
Step 1: Collection stock item from Resource Model
The below code snippet is the required command you need to run:
$resource = $objectManager->create('Magento\CatalogInventory\Model\ResourceModel\Stock\Item');
$select = $resource->getConnection()->select()->from($resource->getMainTable());
$stockItems = $resource->getConnection()->fetchAll($select);
foreach($stockItems as $_item){
var_dump($_item->getData());
}
Step 2: Take Resource Model of StockItem
By that command, you will take Resource Model of StockItem
by using $objectManager
Step 3: Set the select query
Set the select query via connection object
. Then you will retrieve the data from the database by the command $select query
.
Wrap up
That’s all about three steps to get the stock item list in Magento 2. I hope that this tutorial works for you. Let me know if you have any issue following it. I will try best to solve your probem and bring more useful guides in the next posts.
Thanks for reading!
Enjoyed the tutorial? Spread it to your friends!
Sam Thomas
CEO and Founder of Mageplaza. Pursueing a simple and healthy lifestyle. A friend, a husband and a dad of two children, a trainer and an influencer wannabe. He is a big fan of sports and travel, also.
Featured Extensions
People also searched for
- magento 2 how to get stock items list
- magento 2 get out of stock items
- magento 2 get in stock items
- magento 2 get stock items
- how to get out of stock items in magento 2
- magento 2 get product quantity
- magento 2 get product stock status
- magento 2 get product stock quantity
- magento 2 get product qty
- magento 2 update product stock programmatically
- magento 2 get product stock item
- magento 2 get stock qty
- magento 2 update stock programmaticall
- 2.2.x, 2.3.x, 2.4.x