How to get simple products of configurable product in Magento 2

Today we are going to talk about how to get a simple product of a configurable product programmatically in Magento 2. This programming tutorial explains in detail, how to get a simple product id from a configurable product. By following this example you can get the simple 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!