Magento 2: Get Associated Simple Products from Configurable Product

The configurable product in Magento 2 allows the store owner to create a product with a different type of variation, for example T-shirt with different colour (i.e, Red, Green, Blue ), size (i.e, Small, Medium, Large), etc. Those variations nothing but a simple products with unique SKU, inventory, price, etc. In the frontend, the information of the configurable product will be shown in the product detail page and all the associated simple products will be shown as a dropdown or swatches.

For instance, a T-shirt is a configurable product and it has different sizes like small, medium and large. This size option is associated with three different simple products and those simple products have unique SKU, inventory and may have different price based on size. In the product detail page, by default, the T-shirt information will be displayed with this size variation, While we select the option, the selected product SKU and price will be changed in the detail page. This is a default functionality in Magento. But Sometimes like customizing the native feature of Magento 2, we may require to get those associated simple products from the configurable product within the product detail page or somewhere else in the website. That time, this simple article will help you to get the associated simple products from the configurable products programmatically.

In order to get the product information in Magento 2, we might need to inject the object of the \Magento\Catalog\Model\ProductRepository class in the constructor of our module class.

Using Dependency Injection (DI)

Here is the example code to get the associated simple products from the configurable products using dependency injection.

protected $productRepository;
...

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

public function getAssociatedSimpleProduct($configurable_product_id) {
    $configurableProduct = $this->productRepository->getById($configurable_product_id);
    $children = $configurableProduct->getTypeInstance()->getUsedProducts($configurableProduct);
    foreach ($children as $child){
        echo "Child Product Id: " . $child->getId();
        echo "Child Product SKU: " . $child->getSku();
    }
}

Using Object Manager

Here is the example code to get the associated simple products from the configurable product in Magento 2 using object manager.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$configurable_product_id = 68;
$configurableProduct = $objectManager->get('Magento\Catalog\Model\ProductRepository')->getById($configurable_product_id);

$children = $configurableProduct->getTypeInstance()->getUsedProducts($configurableProduct);
foreach ($children as $child){
    echo "Child Product Id: " . $child->getId();
    echo "Child Product SKU: " . $child->getSku();
}

Note: The above code is tested in Magento 2.3.

Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *