2

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?

2

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();
}
| improve this answer | |
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];
}
| improve this answer | |
  • then how can i get the product parent name? – simple guy Feb 20 '18 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 '18 at 8:10
0

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

| improve this answer | |
  • not returning the parents name – simple guy Feb 20 '18 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 '18 at 7:57
  • from order collection in Magento_Customer/view/frontend/templates/account/order/history.phtml – simple guy Feb 20 '18 at 8:01
  • what Magento version are you running? I don't have that in 2.1.11 – steros Feb 20 '18 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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