I need to change the status of the stock to "in_stock" and return the simple product to salable quantity 1 when a refund is made in the case that the last simple product (color = black) of a configurable has been purchased from the store and has been returned.

My code:

foreach ($creditmemo->getAllItems() as $item) {
                    $productId = $item->getProductId();
                    $product = $this->_productFactory->create()->load($productId);
                    $productsSimple = $product->getTypeInstance()->getUsedProducts($product);
                    foreach($productsSimple as $simple){
                        $sku = $simple->getSku();
                        $stockItem = $this->_stockRegistry->getStockItemBySku($sku);
                        $qtyStock = $stockItem->getData('qty');
                        if($qtyStock  == 0){
                            $qty = 1;
                            $this->_stockRegistry->updateStockItemBySku($sku, $stockItem);

Expected result:

I make a purchase from the store of a product with salable quantity 1 therefore it decreases to 0. Then I make a refund and this same product should reappear with salable quantity 1 and the stock status in "in_stock"

This is not working and I have been working for about a week. Please, I thank whoever helps me.

  • I recommend you update your question with more info. (1) Update your question's version flag with your exact Magento version; for example, 2.4.2-p1. (2) Be precise as to what's currently happening. Do both saleable quantity and stock status fail to update? Does one update but not the other? (3) How is inventory configured in admin, especially but not only "Automatically Return Credit Memo Item to Stock?" (4) When you issue a credit memo, are you selecting the "Return to Stock" checkbox? The more information you can provide, the better the chances of getting to the bottom of the problem.
    – kookaburra
    Sep 24, 2021 at 11:44

1 Answer 1


You must make sure that all the default plugins related to magento 2 inventory functionality are enabled. Then Reindex all the products again.

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.