Retrieve All Child Items of Bundle Products in Magento 2

Retrieve All Child Items of Bundle Products in Magento 2
To retrieve all child items of a bundle product in Magento 2, use the Magento\Bundle\Api\ProductLinkManagementInterface interface. The getChildren() method fetches associated child products based on the bundle product’s SKU.
Table Of Content
Retrieve All Child Items of Bundle Products in Magento 2
To retrieve all child items of a bundle product in Magento 2, use the Magento\Bundle\Api\ProductLinkManagementInterface
interface. This interface provides the getChildren()
method, which fetches the associated child products based on the bundle product's SKU.
Method Signature:
public function getChildren($productSku, $optionId = null);
Parameters:
$productSku
: The SKU of the bundle product.$optionId (optional)
: The specific option ID within the bundle. If omitted or set to null, all child items are retrieved.
Example Implementation:
<?php
namespace Vendor\Module\Model;
use Magento\Bundle\Api\ProductLinkManagementInterface;
use Psr\Log\LoggerInterface;
use Magento\Bundle\Api\Data\LinkInterface;
class BundleProductChildren
{
private $productLinkManagement;
private $logger;
public function __construct(
ProductLinkManagementInterface $productLinkManagement,
LoggerInterface $logger
) {
$this->productLinkManagement = $productLinkManagement;
$this->logger = $logger;
}
/**
* Retrieve child items of a bundle product by SKU.
*
* @param string $bundleSku
* @return LinkInterface[]|null
*/
public function getBundleChildren($bundleSku)
{
try {
return $this->productLinkManagement->getChildren($bundleSku);
} catch (\Exception $e) {
$this->logger->error('Error fetching bundle children: ' . $e->getMessage());
return null;
}
}
}
This approach ensures you can programmatically access and manage the child items of bundle products in Magento 2 efficiently.
Tip
To enhance your eCommerce store’s performance with Magento, focus on optimizing site speed by utilizing Emmo themes and extensions. These tools are designed for efficiency, ensuring your website loads quickly and provides a smooth user experience. Start leveraging Emmo's powerful solutions today to boost customer satisfaction and drive sales!
FAQs
How Can I Retrieve Child Items of a Bundle Product in Magento 2?
You can use the getChildren()
method from Magento\Bundle\Api\ProductLinkManagementInterface
to fetch child items.
What PHP Code Can I Use to Get Child Items of a Bundle Product?
Use the following PHP snippet to retrieve child items:
$productSku = 'example-bundle-sku';
$childItems = $this->productLinkManagement->getChildren($productSku);
foreach ($childItems as $child) {
echo $child->getSku();
}
What Parameters Does getChildren() Accept?
The getChildren()
method takes two parameters:
$productSku
- The SKU of the bundle product.$optionId
(optional) - The specific option ID to filter child items. If omitted, all child items are retrieved.
How Can I Handle Errors While Fetching Child Items?
Use a try-catch block to catch exceptions:
try {
$childItems = $this->productLinkManagement->getChildren('example-bundle-sku');
} catch (Exception $e) {
$this->logger->error($e->getMessage());
}
Where Is Bundle Product Data Stored in Magento 2?
Bundle product relationships are stored in the catalog_product_bundle_selection
table in the Magento database.
What Happens If the Bundle SKU Is Incorrect?
If the SKU is invalid, calling getChildren()
will throw an exception. Ensure the SKU exists before making a request.