2

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!

2
  • after your above code execute, can you try indexing programmatically and then check. Jul 11, 2020 at 3:49
  • Hi! Thank you for your reply and time, I tried that before with a sleep(10), just to see if the reindexing is the problem! However that didn't resolve the issue.
    – medjabir
    Jul 11, 2020 at 14:11

2 Answers 2

1

You can use below sample code

$product->setStockData(['qty' => $yourQty, 'is_in_stock' => 1,'manage_stock'=>$your_manage_stock(Not Zero)]);
$product->setQuantityAndStockStatus(['qty' => $yourQty, 'is_in_stock' => 1,'manage_stock'=>$your_manage_stock(Not Zero)]);
$product->save();

And you Get more information refer this link :-

How to update product qty and Stock Status update programmatically by product id : Magento 2

THANKS.

6
  • Hello! Thank you for your reply and time. But have you read my post ? And the code I used ?
    – medjabir
    Jul 11, 2020 at 14:15
  • Please your add briefly and add full code . Jul 12, 2020 at 4:18
  • Post is updated !
    – medjabir
    Jul 12, 2020 at 18:36
  • i am update my code check and try Jul 13, 2020 at 12:09
  • Read my post again! My code works just fine
    – medjabir
    Jul 13, 2020 at 12:57
-1

Please try the below solution:

$product->setStockData(
array(
    'use_config_manage_stock' => 0,
    'manage_stock' => 1,
    'is_in_stock' => 1,
    'qty' => 9
)

);

$product->save();

I hope it helps.

3
  • Hello! Thank you for your reply and time. It's the same code I used ! I don't think you understood what is the issue here.
    – medjabir
    Jul 11, 2020 at 14:16
  • Hello, you have set "manage_stock" to 0 and not specified the quantity which I have corrected in my code and works well for me. Jul 13, 2020 at 4:37
  • Does your product got added to cart after changing the stock status programmatically ? My code also works, the problem is the update takes effect after page reload.
    – medjabir
    Jul 13, 2020 at 12:56

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.