1

I'm trying to update product stock programatically in a store with MSI(multi source inventory) enabled. I want to decrease / increase product quantity based on source_code (inventory source code).

Basically, I'm trying to run a cron on a table having data for increasing & decreasing product quantity, then it'll run code which will update inventory with specified source code.

If some one have better answer, i'd love to receive.

1 Answer 1

0

Here's a root script attached. It's working fine.

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

use Magento\Framework\App\Bootstrap;

require '../app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$sourceItem = $objectManager->get(\Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory::class);
$sourceItem = $sourceItem->create();
$sourceItem->setSku('PATCHE-HOODY-BLACK-MST- L');
$sourceItem->setQuantity(998);
$sourceItem->setSourceCode('default');
$sourceItem->setStatus(1);
$sourceItemSaver = $objectManager->get(\Magento\InventoryApi\Api\SourceItemsSaveInterface::class);
$sourceItemSaver->execute([$sourceItem]);

After operation, you can check results in inventory_source_item table and product grid in admin side. It also helps to update saleable quantity also.

so by using this code, saleable quantity and quantity per source will be updated.

this can also be helpful. https://stackoverflow.com/a/73657391/14146725

Not the answer you're looking for? Browse other questions tagged or ask your own question.