In this article, You will learn about how to update Assigned Inventory sources to bulk products and also you’ll learn how to update assigned inventory sources with the source code. And which files we have to use to assign the inventory sources programmatically.
Please follow these steps one by one:
Step-1: On the Admin Panel, go to the Catalog menu then select the products menu. (catalog > products).See the below Screenshot.

Step-2: Click on edit button to update the source. You will get the all the assigned source on the product edit page. see the below screenshot.

Now suppose we have to update the source with the name test then select the source status from the drop-down and mark the qty as per your requirement. Then click on the Save button.
After that when you go to the product grid where you’ll find the updated sources. See the below screenshot.

Now, the question occurs is that, how to update the assigned source programmatically ?
So, for this you have to find out the files named as SourceItemInterface and SourceItemSaveInterface which you will find on the path “vendor\module-inventory-api\Magento\InventoryApi\Api\Data\SourceItemInterface” and “vendor\module-inventory-api\Magento\InventoryApi\Api\SourceItemsSaveInterface”.
To update the sources use below code:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$sourceItem2 = $objectManager->create('\Magento\InventoryApi\Api\Data\SourceItemInterface');
$sourceItem2->setSku($productSku);
$sourceItem2->setSourceCode($sourceCode);
$sourceItem2->setQuantity($productQty);
$sourceItem2->setStatus($this->status);
$sourceItemSave = $objectManager->get('\Magento\InventoryApi\Api\SourceItemsSaveInterface');
$sourceItemSave->execute([$sourceItem2]);
Use this above code from where you want to update the soucres.
For this you have to provide a four params like:
1: productsku
2: sourcecode
3:productQty
4:status
Hence the sources will get updated.

Be the first to comment.