Is there any way I can fetch all the variants of a configurable products from a product collection in Magento 2? Thanks!

I would like to return the products including the attribute options and and child products as well. So far, I have the code from \Magento\Catalog\Model\ProductRepository::getList()

public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
    /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
    $collection = $this->collectionFactory->create();

    foreach ($this->metadataService->getList($this->searchCriteriaBuilder->create())->getItems() as $metadata) {
    $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
    $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');

    //Add filters from root filter group to the collection
    foreach ($searchCriteria->getFilterGroups() as $group) {
        $this->addFilterGroupToCollection($group, $collection);
    /** @var SortOrder $sortOrder */
    foreach ((array)$searchCriteria->getSortOrders() as $sortOrder) {
        $field = $sortOrder->getField();
            ($sortOrder->getDirection() == SortOrder::SORT_ASC) ? 'ASC' : 'DESC'

    $searchResult = $this->searchResultsFactory->create();
    return $searchResult;

However, this collection does not retrieve the attribute options from configurable products.

  • in Magento1 or Magento2?
    – sandip
    Sep 12, 2017 at 5:18
  • in Magento 2 sir. Sorry just updated the question. Sep 12, 2017 at 5:41

1 Answer 1


You can do it like this


protected $productRepository;

public function __construct(
    \Magento\Catalog\Model\ProductRepository $productRepository,
) {
    $this->productRepository = $productRepository;

public function execute()
    $id = 1; // Configurable Product ID
    $product = $this->productRepository->getById($id);

    // \Magento\ConfigurableProduct\Model\Product\Type\Configurable
    $assoProducts = $product->getTypeInstance()->getUsedProducts($product);

    // Retrieve used product attributes
    $variantsUsed = $product->getTypeInstance()->getUsedProductAttributes($product);


You may be interested in some below functions

getUsedProductAttributeIds() // Retrieve identifiers of used product attributes
getParentIdsByChild() // Retrieve parent ids array by required child
getConfigurableAttributes() // Retrieve configurable attributes data
getConfigurableAttributesAsArray() // Retrieve Configurable Attributes as array
getUsedProductIds() // retrieve subproducts identifiers

For More you can refer the model class


  • Thanks! this is a great help. But can this configurable options be extended in the to the collection? \Magento\Catalog\Model\ProductRepository::getList() ? Sep 12, 2017 at 7:28
  • You can call the list and filter configurable product type from it. Then in loop you can use the above logic. I don't think you can directly extend list to get for all.
    – Priyank
    Sep 13, 2017 at 9:46

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.