Back to Top

Magento 2 Update Assigned Source Inventory from Admin end and programmatically

Updated 13 July 2022

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.

Selection_001

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.

Selection_002

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.

Start your headless eCommerce
now.
Find out More

After that when you go to the product grid where you’ll find the updated sources. See the below screenshot.

Selection_003

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.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home