Get / Update Stock Items in Magento 2

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

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){

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!

Image Description
Hello, I'm the Chief Technology Officer of Mageplaza, and I am thrilled to share my story with you. My deep love and passion for technology have fueled my journey as a professional coder and an ultra-marathon runner. Over the past decade, I have accumulated extensive experience and honed my expertise in PHP development.

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.3.x, 2.4.x