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 />;
}
}
}
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 />;
}
}
}