5

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

1

3 Answers 3

1

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

4
1

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);
2
  • it is working fine only when we keep $access_token as static, if we generate it dynamically using (rest/V1/integration/admin/token) with curl method then it gives following error {"message":"The consumer isn't authorized to access %resources.","parameters":{"resources":"Magento_InventoryApi::source"}}
    – Alam Zaib
    Apr 23, 2019 at 9:02
  • any idea that why it is giving authorized error for dynamic token?
    – Alam Zaib
    Apr 23, 2019 at 9:03
0

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.

5
  • I have tried the following but it is not working $sourceInterface = $objectManager->get('\Magento\InventoryApi\Api\SourceItemsSaveInterface')->create(); $sourceItems[] = ['sku'=>'00001','source_code'=>'source1',"quantity"=> 50,"status"=> 1]; $sourceItems[] = ['sku'=>'00001','source_code'=>'source2',"quantity"=> 40,"status"=> 1]; $sourceItems[] = ['sku'=>'00001','source_code'=>'source3',"quantity"=> 20,"status"=> 1]; $sourceInterface->execute($sourceItems); Jan 8, 2019 at 7:26
  • what you are doing is trying to save an array of data: ['sku'=>'00001','source_code'=>'source1',"quantity"=> 50,"status"=> 1] but an array of \Magento\InventoryApi\Api\Data\SourceItemInterface[] objects should be provided Jan 8, 2019 at 19:07
  • Also tried this $sourceItem = $objectManager->get('\Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory')->create(); $sourceItem->setSourceCode('source1'); $sourceItem->setSku('00001'); $sourceItem->setQuantity(30); $sourceItem->setStatus(1); $sourceItems[] = $sourceItem; $sourceInterface->execute($sourceItems); Jan 9, 2019 at 9:09
  • 1
    Create object of SourceItemInterface and set values like QTY, Source etc and pass as an array like this $this->sourceItemsSave->execute([$sourceItem]);
    – Arshad M
    Apr 24, 2019 at 13:26
  • @Arshad as per last comment we can add one source data. how to save multiple at once Feb 11, 2022 at 8:50

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.