1

I have configurable products and its child items in Magento 2.4 admin panel I have 3 child items for one configurable parent. If I set out of stock for 2 items and save then parent would be in in stock. If I save child items with out of stock then when I check status of parent stock status it automatically stock to out of stock because all child items are out of stock so parent automatically become out of stock so I need to check the which function doing this stuff ?

I looked at into files save() function is the responsible into below file location to doing above stuff ?

\vendor\magento\module-catalog-inventory\Model\Stock\StockItemRepository.php



 
2
  • can you specify what actual you are looking? Commented Jul 29, 2021 at 4:36
  • @DhirenVasoya I want to put my custom code over there so I am finding that function Commented Jul 29, 2021 at 14:49

1 Answer 1

1
+50

Magento has observer named SaveInventoryDataObserver under vendor\magento\module-catalog-inventory\Observer\SaveInventoryDataObserver.php

In execute function of this observer, it triggers $this->processParents($product); which process stock data for parent products.

private function processParents(Product $product)
{
    foreach ($this->parentItemProcessorPool as $processor) {
        $processor->process($product);
    }
}

As you can see in the magento-configurable-product/etc/di.xml

<type name="Magento\CatalogInventory\Observer\SaveInventoryDataObserver">
        <arguments>
            <argument name="parentItemProcessorPool" xsi:type="array">
                <item name="configurable" xsi:type="object"> Magento\ConfigurableProduct\Model\Inventory\ParentItemProcessor</item>
            </argument>
        </arguments>
</type>

this will further hit the magento-configurable-product/Model/Inventory/ParentItemProcessor.php

This class has below function processStockForParent which might be responsible for this(parent stock status it automatically stock to out of stock).

private function processStockForParent(int $productId)
    {
        $criteria = $this->criteriaInterfaceFactory->create();
        $criteria->setScopeFilter($this->stockConfiguration->getDefaultScopeId());

        $criteria->setProductsFilter($productId);
        $stockItemCollection = $this->stockItemRepository->getList($criteria);
        $allItems = $stockItemCollection->getItems();
        if (empty($allItems)) {
            return;
        }
        $parentStockItem = array_shift($allItems);

        $childrenIds = $this->configurableType->getChildrenIds($productId);
        $criteria->setProductsFilter($childrenIds);
        $stockItemCollection = $this->stockItemRepository->getList($criteria);
        $allItems = $stockItemCollection->getItems();

        $childrenIsInStock = false;

        foreach ($allItems as $childItem) {
            if ($childItem->getIsInStock() === true) {
                $childrenIsInStock = true;
                break;
            }
        }

        if ($this->isNeedToUpdateParent($parentStockItem, $childrenIsInStock)) {
            $parentStockItem->setIsInStock($childrenIsInStock);
            $parentStockItem->setStockStatusChangedAuto(1);
            $this->stockItemRepository->save($parentStockItem);
        }
    }

For more information refer this commit - https://github.com/magento/magento2/commit/a52d069900777c291d27b2e20c122970b7aaecca

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.