private function loadVariationsFromSearchIndex(Product $parentProduct, array $attributes)
    {
        $documentSource = $parentProduct->getDocumentSource();
        $childrenIds    = isset($documentSource['children_ids']) ? $documentSource['children_ids'] : [];
        $variation      = false;

        if (!empty($childrenIds)) {
            $childrenIds = array_map('intval', $childrenIds);

            $productCollection = $this->productCollectionFactory->create();
            $productCollection->addIdFilter($childrenIds);

            $configurableAttributes = $this->getAttributesFromConfigurable($parentProduct);
            $allAttributesArray     = [];

            foreach ($configurableAttributes as $attribute) {
                foreach ($attribute->getOptions() as $option) {
                    $allAttributesArray[$attribute['attribute_code']][] = (int) $option->getValue();
                }
            }

            $resultAttributesToFilter = array_merge($attributes, array_diff_key($allAttributesArray, $attributes));

            $this->addFilterByAttributes($productCollection, $resultAttributesToFilter);

            $variationProduct = $productCollection->getFirstItem();

            if ($variationProduct && $variationProduct->getId()) {
                $variation = $this->productRepository->getById($variationProduct->getId());
            }
        }

        return $variation;
    }
private function loadVariationsFromSearchIndex(Product $parentProduct, array $attributes)
    {
        $documentSource = $parentProduct->getDocumentSource();
        $childrenIds    = isset($documentSource['children_ids']) ? $documentSource['children_ids'] : [];
        $variation      = false;

        if (!empty($childrenIds)) {
            $childrenIds = array_map('intval', $childrenIds);

            $productCollection = $this->productCollectionFactory->create();
            $productCollection->addIdFilter($childrenIds);

            $configurableAttributes = $this->getAttributesFromConfigurable($parentProduct);
            $allAttributesArray     = [];

            foreach ($configurableAttributes as $attribute) {
                foreach ($attribute->getOptions() as $option) {
                    $allAttributesArray[$attribute['attribute_code']][] = (int) $option->getValue();
                }
            }

            $resultAttributesToFilter = array_merge($attributes, array_diff_key($allAttributesArray, $attributes));

            $this->addFilterByAttributes($productCollection, $resultAttributesToFilter);

            $variationProduct = $productCollection->getFirstItem();

            if ($variationProduct && $variationProduct->getId()) {
                $variation = $this->productRepository->getById($variationProduct->getId());
            }
        }

        return $variation;
    }
private function loadVariationsFromSearchIndex ( Product $ parentProduct , array $ attributes ) { $ documentSource = $ parentProduct -> getDocumentSource ( ) ; $ childrenIds = isset ( $ documentSource [ 'children_ids' ] ) ? $ documentSource [ 'children_ids' ] : [ ] ; $ variation = false ; if ( ! empty ( $ childrenIds ) ) { $ childrenIds = array_map ( 'intval' , $ childrenIds ) ; $ productCollection = $ this -> productCollectionFactory -> create ( ) ; $ productCollection -> addIdFilter ( $ childrenIds ) ; $ configurableAttributes = $ this -> getAttributesFromConfigurable ( $ parentProduct ) ; $ allAttributesArray = [ ] ; foreach ( $ configurableAttributes as $ attribute ) { foreach ( $ attribute -> getOptions ( ) as $ option ) { $ allAttributesArray [ $ attribute [ 'attribute_code' ] ] [ ] = ( int ) $ option -> getValue ( ) ; } } $ resultAttributesToFilter = array_merge ( $ attributes , array_diff_key ( $ allAttributesArray , $ attributes ) ) ; $ this -> addFilterByAttributes ( $ productCollection , $ resultAttributesToFilter ) ; $ variationProduct = $ productCollection -> getFirstItem ( ) ; if ( $ variationProduct && $ variationProduct -> getId ( ) ) { $ variation = $ this -> productRepository -> getById ( $ variationProduct -> getId ( ) ) ; } } return $ variation ; }
Load variations for a given product with data coming from the search index.

@param Product $parentProduct Parent Product
@param array   $attributes    Attributes

@return bool|\Magento\Catalog\Api\Data\ProductInterface
Load variations for a given product with data coming from the search index .