How To

How to Get Child Items of Bundle Products in Magento 2

Hello Magento Friends,

I am again back with another useful Magento solution for you all. Today it’s about Getting Child Items of Bundle Products in Magento 2.

Bundled products are a group of simple or virtual products that are bought together. You can Create Bundled Products Programmatically in Magneto 2. Bundled products are a combination of various child products. To get all the child items of bundled products, you can follow the below steps.

Steps to Get Child Items of Bundle Products in Magento 2:

Step 1: Create a file at the below path

app\code\Vendor\Extension\Model\Bundlechildproduct.php

And add the below code

<?php
namespace Vendor\Extension\Model;

use Exception;
use Psr\Log\LoggerInterface;
use Magento\Bundle\Api\ProductLinkManagementInterface;

class  Bundlechildproduct
{ 
    private $logger;

    private $productLinkManagement;

    public function __construct(
        LoggerInterface $logger,
        ProductLinkManagementInterface $productLinkManagement)
    {
        $this->logger = $logger;
        $this->productLinkManagement = $productLinkManagement;
    }

    public function getBundleChildrenItems()
    {
        $sku = 'YOUR_PRODUCT_SKU'; 
        try
        {
            $items = $this->productLinkManagement->getChildren($sku);
        }
        catch (Exception $exception)
        {
            throw new Exception($exception->getMessage());
        }

        return $items;
    }
}

Step 2: Call from the template or Any PHP class

$items = $this->getBundleChildrenItems();
foreach ($items as $item)
{
    var_dump($item->getData());
}

Conclusion:

This way you can fetch child items from the bundled products in Magento 2. In case you come across any error, reach me via the comment box. Share the article with your friends and stay updated!

Happy Coding!

Click to rate this post!
[Total: 3 Average: 3.7]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

View Comments

  • hello this post does not show me anything.
    I want to see if a bundle product that contains 3 simple products, if one is out of stock that shows that the bundle product is out of stock

    • Make sure you have created other required files for the extension and the extension is installed and enabled.

Recent Posts

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

5 days ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

1 week ago

Managing Browser Events and Navigation in Shopify Remix: useBeforeUnload, useHref, and useLocation Hooks

Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…

1 week ago

Ultimate Guide to Hiring a Top Shopify Development Agency

Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…

2 weeks ago