1

In order to fetch all associated products of configurable product. I used this code

$childProducts = $configurableproduct->getTypeInstance()->getUsedProducts($configurableproduct);

It return the collection of child products having status In Stock

Any help, knowledge sharing and experience would be highly appreciated.

0

You can look for following file.

vendor/magento/module-configurable-product/Model/Product/Type/Configurable.php

There is a function as follow.

private function loadUsedProducts(\Magento\Catalog\Model\Product $product, $cacheKey, $salableOnly = false)
    {
        $dataFieldName = $salableOnly ? $this->usedSalableProducts : $this->_usedProducts;
        if (!$product->hasData($dataFieldName)) {
            $usedProducts = $this->readUsedProductsCacheData($cacheKey);
            if ($usedProducts === null) {
                $collection = $this->getConfiguredUsedProductCollection($product, false);
                if ($salableOnly) {
                    $collection = $this->salableProcessor->process($collection);
                }
                $usedProducts = array_values($collection->getItems());
                $this->saveUsedProductsCacheData($product, $usedProducts, $cacheKey);
            }
            $product->setData($dataFieldName, $usedProducts);
        }

        return $product->getData($dataFieldName);
    }

Here it is calling a protected method getConfiguredUsedProductCollection with second argument as false which is skipStockFilter.

Magento by default pass it false, this will be true only if you have shown out of stock product in store. If that is set to no, you can not get out of stock products by using this feature.

What you can do is, copy this file into your custom extension and change the namespace accordingly, inject it in the constructor where you want to get simple products and make above variable true which magento is passing false as i explained, Also in your copied file, remove to get and set data to cache or else, it will show incorrect data elsewhere it is getting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.