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; $stockItem->setQty($qty); $stockItem->setIsInStock(true); $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.