Magento 2 Tutorial: Get Child Products of Configurable Product

how to get child product in configurable product magento

Magento 2 offers a lot of great features for online stores, and one of these is the ability to manage products with different variations. Sometimes, though, you might want to see the specific variations (or “child products”) of a main product. If you’ve been wondering how to do that, you’re in the right place. In this guide, we’ll show you a simple way to fetch those child products. Whether you’re an experienced developer or new to Magento 2, this tutorial will help you get the hang of it. Let’s get started!

To get the child product from the configurable product in Magento, we can follow this instruction:

In the order, if using this method:

$items = $order->getAllVisibleItems();

foreach ($items as $item) {

   $item->getProduct()->getData();

}

You just can get the parent product.

However, if you want to get the child products or options from the Configurable product, you must use the code below to add this code to anywhere that you want.

public function getItems($order)

{

  if ($order) {

    $items = $order->getAllVisibleItems();

    foreach ($items as $item) {

      if($item->getProduct()->getTypeId() == 'configurable'){

       $childProduct = $this->getChildProduct($item);

       $childProductId = $childProduct->getData();
      }
    }
  }
}

 

public function getChildProduct($orderItem)

{

  $productId = $orderItem->getProductId();

  $product = Mage::getModel('catalog/product')->load($productId);

  $optionData = unserialize($orderItem->getData('product_options'));

  $childProduct = Mage::getModel('catalog/product_type_configurable')->getProductByAttributes($optionData['info_buyRequest']['super_attribute'], $product);

  return $childProduct;

}

We have shown you how to get the simple product from the Configurable products in Magento. Hope you guys find this concise tutorial useful.

Leave a Reply

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