How To

How to Get Product Multi Source Inventory (MSI) Programmatically in Magento 2

Hello Magento Friends,

In this blog, I will explain How to Get Product Multi Source Inventory (MSI) Programmatically in Magento 2.

First, let’s understand what is Multi Source Inventory (MSI ) in Magento 2.

Magento 2 Multi Source Inventory (MSI)

Multi Source Inventory helps to manage inventory by location. Merchants can link inventories to one or multiple websites without third-party extensions or platforms. MSI improves inventory management irrespective of customer or warehouse location, product type or sales channel.

Let’s get back to the point and find out the method to Get Product Multi Source Inventory (MSI) Programmatically in Magento 2.

Steps to Get Product Multi Source Inventory (MSI) Programmatically in Magento 2:

Step 1:  We are getting data in the root script. Move to the below file path.

magento_root_directory\sourceinventory.php

Now add the code as follows.

<?php
use Magento\Framework\App\Bootstrap;
use Magento\Store\Model\ScopeInterface;

require './app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
 
try
{ 
   
    $sku = 'simple product'; // item sku
              
    $sourceinventorys = $objectManager->get('\Magento\InventoryCatalogAdminUi\Model\GetSourceItemsDataBySku');
    $data = $sourceinventorys->execute($sku);

    foreach ($data as $source)
    {
         echo "<pre>";
         print_r($source);
         echo "<pre>";
    }
}catch(\Exception $e)
{
    print_r($e->getMessage());
}

Output:

Conclusion:

Hence, this way, you can Get Product Multi Source Inventory (MSI) Programmatically in Magento 2. If you have any doubts, let me know through the comment box and share the article with your friends. See you in the next tutorial; till then, stay with us!

Happy Coding!

Click to rate this post!
[Total: 6 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

Generating Thumbnails with Spatie Media Library in Laravel 11: A Step-by-Step Guide

Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…

13 hours ago

Enhancing Web Application Security with Laravel’s Built-In Features

In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…

2 days ago

Magento 2 Extensions Digest October 2024 (New Release & Updates)

October was an exciting month for MageComp! From significant updates across our Magento 2 extension…

2 days ago

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

1 week ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

2 weeks ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

2 weeks ago