In Magento 2.3 many stock related interfaces are deprecated since MSI is introduced. So I am trying to programmatically set that configurable product is in stock when any if its variations gets back in stock (using cron job that reads inventory data from ERP). The first thing I did was to set quantities and in stock status of each variation using SourceItemsSaveInterface. And this worked just fine. But then I tried to set the in stock status of the configurable product (it was still out of stock) so that customers can add this product to cart. I used the similar code as for each variation, which looked something like this:
/** @var SourceItemInterface $sourceItem */
$sourceItem = $this->sourceItemFactory->create();
$sourceItem->setSourceCode('default'); //We have only one (default) source
$sourceItem->setSku($sku); //Configurable product SKU
$sourceItem->setQuantity($totalQuantity); //Total quantity is the sum of all variation quantities
$sourceItem->setStatus($totalQuantity > 0 ? SourceItemInterface::STATUS_IN_STOCK : SourceItemInterface::STATUS_OUT_OF_STOCK);
$this->sourceItemsSaveInterface->execute([$sourceItem]);
Now after this code has been executed, the configurable product status is still not changed to In Stock for configurable products that should be in stock. So I checked some related database tables. And I found that cataloginventory_stock_item records are updated for variations, but not for the configurable product itself. Then I checked the inventory_source_item table, and found that there are valid records there for both configurable product and all of its variations.
So with the code above I managed only to insert a record into inventory_source_item table, but I did not manage to update cataloginventory_stock_item table, which I think Magento 2.3 still uses to check if product is out of stock when adding product to the cart. The final result is that customers are unable to add related product variation to the cart even when it is in stock.
My question is: what am I missing and how to set that configurable product is in stock programmatically to overcome this problem?
Hi @predragsto51a5
Please try to reindex by using below command:
bin/magento indexer:reindex
it may help you to resolve issue.
If issue resolve, please click on 'Kudos' & Accept as Solution!
It does not help. I tried that first of course
Hi @predragsto51a5,
Can you please check below link
https://www.google.com/amp/s/webkul.com/blog/update-product-stock-programmatically-in-magento2/amp/
Hope this helps you.
Problem Solved! Click Kudos & Accept as Solution!
@Nishu Jindal wrote:Hi @predragsto51a5,
Can you please check below link
https://www.google.com/amp/s/webkul.com/blog/update-product-stock-programmatically-in-magento2/amp/
Thank you for your reply. The thing is that the solution that you proposed (as well as many others that I found by googling this issue for hours and hours before posting this topic) uses \Magento\CatalogInventory\Api\StockRegistryInterface which is deprecated in Magento 2.3 and replaced with Multi Source Inventory (MSI). That is why I tried to implement this using MSI, as you can see from the code above, but it does not seem to work...
Any update on this predragsto51a5?
I'm trying to decipher the new MSI structure as well. Although, I want to make changes directly to the database to update product stock.
My past understanding is that inventory stock qty is stored in both cataloginventory_stock_item and cataloginventory_stock_status.
Making changes to these (Qty and stock status in each) would be fine with updating the quantities.
I believe now we have to make updates to the source. This also includes QTY and stock status.
I believe these are stored in inventory_source_item by sku/source.
Thoughts on this structure? I'm not sure why your function isn't fully updating these tables correctly.
If you want to easily update quantity per source directly from grid, with no technical skills you can use Magento 2 Product Grid Editor. Except for quantity per source updating, it allows you to do multiple actions in the product grid to optimize order management.