I’m trying to update product quantities when something sells or is revised on another selling channel. I have a platform notification which is received, and everything works fine except when I’m trying to revise an out of stock product. In this situation, the quantity will change, buy the salable quantity will not. If I then locate the product in Admin->Catalog->Products
and save it without making any changes, the salable quantity will update. What am I doing wrong?
My code is below.
public function updateProduct(string $sku, int $qty): void
{
$sourceItem = $this->sourceItemFactory->create();
$sourceItem->setSourceCode('default');
$sourceItem->setSku($sku);
$sourceItem->setQuantity($qty);
if ($qty > 0) {
$sourceItem->setStatus(SourceItemInterface::STATUS_IN_STOCK);
} else {
$sourceItem->setStatus(SourceItemInterface::STATUS_OUT_OF_STOCK);
}
$this->sourceItemsSaveInterface->execute([$sourceItem]);
$this->reIndex();
}