1

I want to load bundle child products from bundle product parent id in Magento 2.

anyone know how can we achieve this?

Thanks :)

2 Answers 2

3

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);
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.