I want to load bundle child products from bundle product parent id in Magento 2.
anyone know how can we achieve this?
Thanks :)
You can get the type instance in your bundle product, then get the children IDs, as below.
$typeInstance = $product->getTypeInstance();
$requiredChildrenIds = $typeInstance->getChildrenIds($product->getId(), true);
Try this code
protected $productdata;
public function __construct(
............................................
\Magento\Catalog\Model\ProductFactory $productdata,
............................................
) {
............................................
$this->productdata = $productdata;
............................................
}
.......................................................................
public function getBundleProudct(){
//$prd is the current product collection or you add your product object
if ($prd->getTypeId() == 'bundle') {
$typeInstance = $prd->getTypeInstance();
$requiredChildrenIds = $typeInstance->getChildrenIds($prd->getId(), false);
//print_r($requiredChildrenIds);
foreach ($requiredChildrenIds as $Childrenkey => $Childrenvalue) {
foreach ($Childrenvalue as $key => $value) {
//print_r($value);
$child = $this->productdata->create()->load($value);
print_r($child->getName());
echo "<br/>";
}
}
}
I Hope This Helps You