0

I need to fetch the stock status (In Stock/Out of Stock) of all the stores for the single product.

I have configured MSI. Lets say am in store1. In product view page I should be able to get the stock status of other stores(store2)as well.

Can anyone help me with this ASAP?

5
  • 2
    stock is not something that is in store scope. it's on global scope Commented Mar 20, 2019 at 12:27
  • In Magento 2.3 we have MSI I have two sources both assigned to different stores. I need to get the stock status of all that Stock Source for a single product.
    – Quasar M
    Commented Mar 20, 2019 at 13:05
  • Ah true. but in MSI context, stock is still not in the store-scope. sources are for websites. Commented Mar 20, 2019 at 13:08
  • Ohhh ok. My requirement is, I have two sources(source1 and source2). I need to fetch the "Source Item Status" of both the Sources programmatically.
    – Quasar M
    Commented Mar 20, 2019 at 13:33
  • Can you please help me to get "Source Item Status" of both the Sources programmatically @PhilippSander ?
    – Quasar M
    Commented Mar 20, 2019 at 15:02

5 Answers 5

11

Intro

MSI stock works differently from the previous CatalogInventory stock. To explain how to get the stock properly i first need to explain how MSI works.

However i will state this beforehand, it is impossible to get a different stock based on storeviews. It is only possible to get it per website, as i will explain below:

In Magento 2.3 when using MSI you can have multiple "Stocks" which each have their own "Sources".

Stock explained

A "Stock" is pretty much your total inventory for one or more websites. Each "Stock" can be assigned to multiple websites, however a website can only have a single "Stock" as this encompasses all of your stock for that website, regardless of the "Source". Within a "Stock" you can have multiple "Sources".

Sources explained

A "Source" is pretty much a warehouse, with a location and it's own stock. Each "Source" can be configured per product. It is possible to leave sources empty, however it is not possible to change the stock on a storeview level, it is always global for the websites your source's stock is linked to.

Possible Solution

The comment of Prabhu M. was pretty close but did not explain why you should use this.

Magento\InventoryApi\Api\GetSourceItemsBySkuInterface

This code gets the "Source Items" for a product based on the SKU. It does so for all sources, so if lets say you have 2 sources (warehouses) for a specific website, it will retrieve both of them. It will also do the same for other websites.

Example:

Lets say you have 2 websites.

  1. website1.nl
  2. website2.com

For website 1 you have it linked to the "Stock": Stock NL

For "Stock NL" you have 3 sources linked to it:

  1. Warehouse Rotterdam (33 stock) [code: rotterdam]
  2. Warehouse Amsterdam (0 stock) [code: amsterdam]
  3. Warehouse Leiden (5 stock) [code: leiden]

For website 2 you have it linked to the "Stock": Stock EN

For "Stock EN" you have 1 sources linked to it:

  1. Warehouse New York (0 stock) [code: new_york]

To get the data above you can use the following code:

use Magento\Inventory\Model\SourceItem\Command\GetSourceItemsBySku;
...    
public function __construct(
    GetSourceItemsBySku $getSourceItemsBySku
) {
    $this->getSourceItemsBySku = $getSourceItemsBySku;
}

...
function doSomething($product) {
    $sourceItems = $this->getSourceItemsBySku->execute($product->getSku());

    foreach ($sourceItems as $sourceItemId => $sourceItem) {
        print $sourceItem->getSku() . ','; 
        print $sourceItem->getSourceCode() . ','; // The actual source code, e.g: warehouse_rotterdam
        print $sourceItem->getStatus() . ','; // wether it is sellable
        print $sourceItem->getQuantity() . '<br/>'; // the quantity for the current source
    }
}
...

This will return the follow data:

testproduct, rotterdam, 1 (true), 33

testproduct, amsterdam, 0 (false), 0

testproduct, leiden, 1 (true), 5

testproduct, new_york, 1 (false), 0

In case of Stock NL your total quantity would be 38 and it would be sellable.

In case of Stock EN your total quantity would be 0 and it would not be sellable. (as it has no sources with stock)

Do note that this does not provide you with any indication of the website it is linked to.

If you know which sources you want to get it for you could use the following to get the stock:

use Magento\InventorySourceDeductionApi\Model\GetSourceItemBySourceCodeAndSku;
...
public function __construct(
    GetSourceItemBySourceCodeAndSku $getSourceItemBySourceCodeAndSku
) {
    $this->getSourceItemBySourceCodeAndSku = $getSourceItemBySourceCodeAndSku;
}
...
public function doSomething($product, $source) {
    $sourceItem = $this->getSourceItemBySourceCodeAndSku->execute($sourceCode, $itemSku);

    print $sourceItem->getSku() . ',';
    print $sourceItem->getSourceCode() . ','; // The actual source code, e.g: warehouse_rotterdam
    print $sourceItem->getStatus() . ','; // wether it is salable
    print $sourceItem->getQuantity() . '<br/>'; // the quantity for the 
}
...
1
  • Really useful. Is there a way to link a website to a source? Commented Jul 14, 2020 at 2:25
0

Programmatically you can get stock statuses of multiple source using below code

$sourceItemsBySku = $this->getSourceItemsBySkuInterface->execute($prodSKU);
$sourceData = array();
foreach ($sourceItemsBySku as $sourceItem) {
    $source_code = $sourceItem->getSourceCode();
    $source_status = $sourceItem->getStatus();
    $source_qty = $sourceItem->getQuantity();
    $sourceData[$source_code]['source_status'] = $source_status;
    $sourceData[$source_code]['source_qty'] = $source_qty;
}
return $sourceData;

Instantiate Magento\InventoryApi\Api\GetSourceItemsBySkuInterface interface in constructor.

-1

I could able to get those MSI "source item status" using below REST API but not programmatically.

<baseUrl>/rest/<store_code>/V1/stockStatuses/<prodSKU>
-1

Magento allows to configure Stock and have different stocks on the level of Website, but not Store Views, unless you made customization which adds a possibility to treat Store View as an independent Sales Channel

-1

In general to retrieve the StockStatus on Magento 2.3 and later you need to call service - https://github.com/magento-engcom/msi/tree/2.3-develop/app/code/Magento/InventorySalesApi/Api

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.