I need to be able to programmatically update product stock in my Cron job. I have an array that contains both the SKU of the product needing updating and the Stock value of that product. How can I select a product by SKU and then update the stock?
Magento 2.1 Cron Job – How to Update Product Stock
cronmagento-2.1magento2skustock
Best Answer
I'm not sure about your data array which contains the SKU product and Stock value. So, I will use simple data.
We can update product stock via API:
vendor/magento/module-catalog-inventory/Model/StockRegistry.php
vendor/magento/module-catalog/Model/ProductRepository.php
You can quick test our code lines by using playground:
How can I bootstrap Magento 2 in a test.php script?
http://www.boolfly.com/magento-2-playground/