How to get Child Product of any Config Product in Magento 2.x

In Magento 2.x, there are 6 types of the product ( Go to Link). In which some are the config product, who have some variation( child product ) as: Configurable Product, Bundle Product, and Grouped Product, etc. So in term of that, if you want to get the variation product info (say about SKU) of all these config Products on behalf of the Product ID, use the code as below:

  • For Configurable Product:

[code]
public function getVariation($product)
{
$variationProduct= $product->getTypeInstance()->getUsedProducts($product);
foreach ($variationProduct as $child){
$childproductSKU[]=$child->getSKU();
}
return $childproductSKU;
}
[/code]

  • For Bundle Products:

[code]

public function getVariation($product)
{
$bundleChildCollection = $product->getTypeInstance(true)->getSelectionsCollection(
$product->getTypeInstance(true)->getOptionsIds($product), $product);
foreach($bundleChildCollection as $option)
{
$childproductSKU[] = $option->getSKU();
}

return $childproductSKU;
}
[/code]

  • For Grouped Products: 

[code]

public function getVariation($product) {
$groupedProducts=$product->getTypeInstance(true)->getAssociatedProducts($product);
foreach ($groupedProducts as $child)
{
$groupedProductSKU[]=$child->getSKU();
}
return $groupedProductSKU;
}
[/code]

 

Happy Coding 🙂

 

 

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *