cancel
Showing results for 
Search instead for 
Did you mean: 

How to set qty for multiple sources (Multi source inventory) programmatically

How to set qty for multiple sources (Multi source inventory) programmatically

Hello,

 

Can anyone help me for How to set qty for multiple sources (Multi-source inventory) programmatically in magento 2.

 

Currently, I am using this code but it's not working.

 

$sourceItem = $objectManager->get('\Magento\InventoryApi\Api\Data\SourceInterface')->create();
$sourceItem->setSourceCode('default');
$sourceItem->setSku($productcode);
$sourceItem->setQuantity(111);
$sourceItem->setStatus(1);
$sourceItems[] = $sourceItem;
$sourceItem->execute($sourceItems);

2 REPLIES 2

Re: How to set qty for multiple sources (Multi source inventory) programmatically

Can anyone help to figure out How to Save Multiple Source item data Programmatically in Magento 2.3?

Re: How to set qty for multiple sources (Multi source inventory) programmatically

hi,
 
you can use the code like below to set the qty for multi source inventory. 
 
 
 
<?php

namespace vendor\module;

use Magento\InventoryApi\Api\SourceItemsSaveInterface;
use Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory;

class class_name
{

public function __construct(
SourceItemsSaveInterface $sourceItemsSaveInterface,
SourceItemInterfaceFactory $sourceItemFactory
){
$this->_sourceItemsSaveInterface = $sourceItemsSaveInterface;
$this->_sourceItemFactory = $sourceItemFactory;
}

public function fun_name(){
$sourceItem = $this->_sourceItemFactory->create();
$sourceItem->setSourceCode('default');
$sourceItem->setSku('111');
$sourceItem->setQuantity(2);
$sourceItem->setStatus(1);
$this->_sourceItemsSaveInterface->execute([$sourceItem]);
}
}