Magento 2 Get Child Products of Configurable Product

Today we are going to talk about how to get a child product of a configurable product programmatically in Magento 2. This programming tutorial explains in detail, how to get child product id from configurable product. By following this example you can get the child product’s other data like name. So let’s start with our example.

namespace QaisarSatti\Module\Block;

class Product extends \Magento\Framework\View\Element\Template
{

  protected $_product;  


  public function __construct(
     
        \Magento\Catalog\Model\ProductFactory $_productloader

    ) {


        $this->_productloader = $_productloader;
     
    }
    public function getLoadProduct()
    {
        $product_id=7;
       $configProduct=$this->_productloader->create()->load($product_id);
       $_children = $configProduct->getTypeInstance()->getUsedProducts($configProduct);
foreach ($_children as $child){
    echo "Child Product Ids ".$child->getID()".<br />;
}
    }

}

Qaisar Satti

Hi, I'm Qaisar Satti! I've been a developer for over 20 years, and now I love sharing what I've learned through tutorials and guides. Whether you're working with Magento, PrestaShop, or WooCommerce, my goal is to make your development journey a bit easier and more fun. When I'm not coding or writing, you can find me exploring new tech trends and hanging out with the amazing developer community. Thanks for stopping by, and happy coding!