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 🙂
Praveen Maurya work in e-commerce domain having knowledge of Plugin development in Magento 1.x/2.x, Drupal, Woo-Commerce, PrestaShop, Opencart and other e-commerce solution. When he is not engrossed with anything related to these, he loves to explore new things.