4

I have a product model from order item model, which i call like this:

$product = $orderItem->getProduct();

The problem is one of the order item is a child product from configurable product, i need to retrieve the parent name from this product, is there a way to do this?

4 Answers 4

4

Try this code:

$productId = 1; //this is child product id
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable')->getParentIdsByChild($productId);
if(isset($product[0])){
  $product = $objectManager->create('Magento\Catalog\Model\Product')->load($product[0]);
    echo $product->getName();
}
2

Get Parent Product from Child Id.

protected $_productloader;
protected $configurable;
protected $grouped;
protected $bundleSelection;

public function __construct(
    ...........................................................
    \Magento\Catalog\Model\ProductFactory $_productloader,
    \Magento\ConfigurableProduct\Model\Product\Type\Configurable $configurable,
    \Magento\GroupedProduct\Model\Product\Type\Grouped $grouped,
    \Magento\Bundle\Model\Product\Type $bundleSelection,
    ...........................................................
) {
    ...........................................................
    $this->_productloader = $_productloader;
    $this->configurable = $configurable;
    $this->grouped = $grouped;
    $this->bundleSelection = $bundleSelection;
    ...........................................................
}

...........................................................
...........................................................
...........................................................

public function getParentProductData($productData)
{
    $prdData = '';
    $parentProductId = $this->getParentProductId($productData->getEntityId());
    if ($parentProductId) {
        $prdData = $this->_productloader->create()->load($parentProductId);
    } else {
        $prdData = $this->_productloader->create()->load($productData->getEntityId());
    }
    // parent Product Data
    return $prdData;
}

public function getParentProductId($childId)
{
    $parent_id = 0;
    // configurable product
    $product = $this->configurable->getParentIdsByChild($childId);
    if (isset($product[0])) {
        $parent_id = $product[0];
        return $parent_id;
    }
    // group product
    $groupIds = $this->grouped->getParentIdsByChild($childId);
    if (isset($groupIds[0])) {
        $parent_id = $groupIds[0];
        return $parent_id;
    }
    // bundel product
    $budel_id = $this->bundleSelection->getParentIdsByChild($childId);
    if (isset($budel_id[0])) {
        $parent_id = $budel_id[0];
        return $parent_id;
    }
    return $parent_id;
}
1

Use this code:

protected $_catalogProductTypeConfigurable;

 public function __construct(
    \Magento\Catalog\Block\Product\Context $context,       
    //for getting parent id of simple
    \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $catalogProductTypeConfigurable,
    array $data = []
) {
    //for getting parent id of simple
    $this->_catalogProductTypeConfigurable = $catalogProductTypeConfigurable;
    parent::__construct($context, $data);
}
public function getProductData($id){
    $productId = $orderItem->getProduct()->getId();
    $parentByChild = $this->_catalogProductTypeConfigurable->getParentIdsByChild($productId);
    if(isset($parentByChild[0])){
        //set id as parent product id...
        $id = $parentByChild[0];          
    }
    return $id;     
}

Alternatively, you can use this too:

$productId = $orderItem->getProduct()->getId(); //this is child product id
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable')->getParentIdsByChild($productId);
 if(isset($product[0])){
     //this is parent product id..
     echo $product[0];
}
2
  • then how can i get the product parent name?
    – simple guy
    Feb 20, 2018 at 8:01
  • Use the above mentioned code after your code $product = $orderItem->getProduct(); As it will return product id, now you can load the product name by loading product factory.
    – Manish Joy
    Feb 20, 2018 at 8:10
0

$orderItem->getName() should return the parents name for a configurable product.

4
  • not returning the parents name
    – simple guy
    Feb 20, 2018 at 7:55
  • ok, please provide a bit more context then in your question. where do you get the orderItem from etc.
    – steros
    Feb 20, 2018 at 7:57
  • from order collection in Magento_Customer/view/frontend/templates/account/order/history.phtml
    – simple guy
    Feb 20, 2018 at 8:01
  • what Magento version are you running? I don't have that in 2.1.11
    – steros
    Feb 20, 2018 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.