I have been facing an issue lately, I want to allow some products to be added to cart and ordered (programmatically) even if they're Out of Stock
, I have succeeded in updating both is_in_stock
and stock_status
but the product stock isn't updating right away, the changes only reflect when the page is reloaded.
This is the code I used to update and edit stock data
$product = $this->_productRepository->getById($childProduct->getId());
$product->setStockData([
'stock_id' => 1,
'use_config_manage_stock' => 0,
'manage_stock' => 0,
'is_in_stock' => 1
]);
$product->setQuantityAndStockStatus([
'stock_id' => 1,
'use_config_manage_stock' => 0,
'manage_stock' => 0,
'is_in_stock' => 1
]);
$product->setIsSuperMode(true);
$product->save();
die(var_dump($product->getIsSalable())); //Returns FALSE, however after refresh it returns TRUE.
Any help is appreciated!
sleep(10)
, just to see if the reindexing is the problem! However that didn't resolve the issue.