Magento 2.3 : How to set qty for multiple sources (Multi source inventory) programmatically
-
13-02-2021 - |
Question
I have a site with 3 inventory sources. I want to add and update product inventory to each source by product sku. Please help me
Solution
I don't know you guys get solution or not but here you go.
Here is the solution https://magento.stackexchange.com/a/262873/49826
OTHER TIPS
I just use API call to get the stock created or updated. code is following:
$access_token = '4v81lfihtkqanfdr6798zj352qyuyx1x';
$url = 'http://magento2.local/index.php/rest/V1/inventory/source-items';
$ch = curl_init($url);
$body = json_encode(['sourceItems' =>[
['source_code' => 'default', 'sku' => '24-MB01', 'quantity'=>555, 'status'=>1],
['source_code' => 'la', 'sku' => '24-MB01', 'quantity' => 222, 'status' => 1],
['source_code' => 'default', 'sku' => '24-MB04', 'quantity'=>444, 'status'=>1],
['source_code' => 'la', 'sku' => '24-MB04', 'quantity' => 111, 'status' => 1],
['source_code' => 'default', 'sku' => '24-MB03', 'quantity'=>333, 'status'=>1],
['source_code' => 'la', 'sku' => '24-MB03', 'quantity' => 111, 'status' => 1],
]]);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer ' . $access_token));
$result = curl_exec($ch);
var_dump($result);
You can use SourceItemsSaveInterface interface for these purposes: https://github.com/magento-engcom/msi/blob/2.3-develop/app/code/Magento/InventoryApi/Api/SourceItemsSaveInterface.php
interface SourceItemsSaveInterface
{
/**
* Save Multiple Source item data
*
* @param \Magento\InventoryApi\Api\Data\SourceItemInterface[] $sourceItems
* @return void
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Validation\ValidationException
* @throws \Magento\Framework\Exception\CouldNotSaveException
*/
public function execute(array $sourceItems): void;
}
And pass an array of \Magento\InventoryApi\Api\Data\SourceItemInterface[] as a parameter, each source item represents inventory of specific product on specific source.
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange