Configurable Product - Get child product id

Try the below solution:

<?php
    $productId = 5; //Configurable product ID
    $_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $_product = $_objectManager->create('Magento\Catalog\Model\Product')->load($productId);
    $_childProducts = $_product->getTypeInstance()->getUsedProducts($_product);
    foreach ($_childProducts as $simpleProduct){
        echo $simpleProduct->getId();
    }
?>

Suggestion: Don't use object manager directly in your code as its not a best practice. You need to inject the product model class to your respective class then use it.


Try to use this code :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$configProduct = $objectManager->create('Magento\Catalog\Model\Product')->load($product_id);

$_children = $configProduct->getTypeInstance()->getUsedProducts($configProduct);

foreach ($_children as $child){
    echo $child->getID();
}