This is verified in Magento 2.3.2
public function __construct(
\Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
)
{
$this->stockRegistry = $stockRegistry;
parent::__construct();
}
public function yourMethod() {
$sku = 'ABC123';
$qty = 10;
$stockItem = $this->stockRegistry->getStockItemBySku($sku);
$stockItem->setQty($qty);
$stockItem->setIsInStock((bool)$qty); // this line
$this->stockRegistry->updateStockItemBySku($sku, $stockItem);
}
Update Stock using ObjectManager
Create a sandbox script PHP file
in magento root dir and add below code
require __DIR__ . '/app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$sku = 'demo';
$qty = 10;
$_sourceItemsSaveInterface = $objectManager->get('\Magento\InventoryApi\Api\SourceItemsSaveInterface');
$_sourceItemFactory = $objectManager->get('Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory');
$sourceItem = $_sourceItemFactory->create();
$sourceItem->setSourceCode('default'); // default : stock source
$sourceItem->setSku($sku);
$sourceItem->setQuantity($qty);
$sourceItem->setStatus(1);
$_sourceItemsSaveInterface->execute([$sourceItem]);