Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

Get / Update Stock Items in Magento 2

Vinh Jacker | 12-18-2024

Get Stock Items

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

x
    Jacker

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services