Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find parent id of product in magento

Tags:

magento

here is my code.

 $storeId = Mage::app()->getStore()->getId();
    $session = Mage::getSingleton('checkout/session');
    foreach($session->getQuote()->getAllItems() as $item) 
    {//how to find parent id here}

At the above mentioned comment I want to access parent id of that particular product. Please Help ..

like image 817
Chiragit007 Avatar asked Dec 07 '25 10:12

Chiragit007


2 Answers

Try below code, may be it will help you

if($item->getTypeId() == "simple"){
    $parentIds = Mage::getModel('catalog/product_type_grouped')->getParentIdsByChild($item->getId()); // check for grouped product
    if(!$parentIds)
        $parentIds = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($item->getId()); //check for config product

}
like image 52
Mufaddal Avatar answered Dec 10 '25 10:12

Mufaddal


You can use this to get the parent

foreach(...) {
    $parents = $item->getTypeInstance()->getParentIdsByChild($item->getId());
    //if you need to load the parent
    if(!empty($parents)) {
        $parentProd = Mage::getModel('catalog/product')->load($parents[0]);
        //do something
    }
}
like image 27
Achrome Avatar answered Dec 10 '25 09:12

Achrome



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!