How to get parent product id from Simple Product

This blog will guide you on how to get parent product id for simple product in magento. There are several ways we can achieve this.

Example 1:
In following code assign appropriate value for variable.

$product_id // Current simple product.
// Build Product
$_product = Mage::getModel('catalog/product')->load($product_id);
//  check if product is of simple type then get parent product id
if($_product->getTypeId() == 'simple') {
$configurable_product_model_obj = Mage::getModel(‘catalog/product_type_configurable’);
$parentIdsArray = $configurable_product_model_obj->getParentIdsByChild( $_product->getId() );
}

Example 2:

if($_product->getTypeId() == "simple") {
$configurable_product_model = Mage::getModel("catalog/product_type_configurable");
$parentIdArray = $configurable_product_model->getParentIdsByChild($_product->getId());
if(count($parentIdArray) > 0  AND !isset($parentIdArray[0]) ) {
$parent_Product_Object = Mage::getModel('catalog/product')->load($parentIdArray[0]);
echo " Parent Product ID : ". $parent_Product_Object->getId();
}
}

Example 3:

For the time being, my fix is just to copy the Mage_Catalog_Model_Resource_Eav_Mysql4_Product class into my local code folder and add the method back in. The code for this method is the following:
With the following method added back in, my original code works. I’d like to know if there is a new suggested approach to get a product’s parent, but for the time being, this is what I’ll fall back to.

function getParentProductIds($object) {
$childId = $object->getId();
$groupedProductsTable = $this->getTable('catalog/product_link');
$groupedLinkTypeId = Mage_Catalog_Model_Product_Link::LINK_TYPE_GROUPED;
$configurableProductsTable = $this->getTable('catalog/product_super_link');
$groupedSelect = $this->_getReadAdapter()->select()
->from(array('g'=>$groupedProductsTable), 'g.product_id')
->where("g.linked_product_id = ?", $childId)
->where("link_type_id = ?", $groupedLinkTypeId);
$groupedIds = $this->_getReadAdapter()->fetchCol($groupedSelect);
$configurableSelect = $this->_getReadAdapter()->select()
->from(array('c'=>$configurableProductsTable), 'c.parent_id')
->where("c.product_id = ?", $childId);
$configurableIds = $this->_getReadAdapter()->fetchCol($configurableSelect);
return array_merge($groupedIds, $configurableIds);
}

 

Let me know if you need any help.

 

Share this nice post:

39 thoughts on “How to get parent product id from Simple Product

  1. What’s Happening i’m new to this, I stumbled upon this I’ve found It positively helpful and it has aided me out loads. I’m hoping to contribute & aid other customers like its helped me. Good job.

  2. You actually make it seem so easy with your presentation. I am looking forward for your next post, I will try to get the hang of it!

  3. I got what you think, thanks for swing up. Woh I am cheerful to chance this website through google. Thanks For Share How to get parent product id from Simple Product.

  4. I got what you signify, thanks for putting up. Woh I am glad to find this website through google. Thanks For Share How to get parent product id from Simple Product.

  5. Great goods from you, man. How to get parent product id from Simple Product I have understand your stuff previous to and you’re just extremely wonderful. I actually like what you have acquired here, certainly like what you’re saying and the way in which you say it. You make it entertaining and you still care for to keep it sensible. I can’t wait to read far more from you. This is actually a terrific How to get parent product id from Simple Product informations.

  6. Wonderful goods from you, man. How to get parent product id from Simple Product I’ve understand your stuff previous to and you are just extremely magnificent. I really like what you’ve acquired here, really like what you are saying and the way in which you say it. You make it entertaining and you still take care of to keep it wise. I can’t wait to read much more from you. This is really a great How to get parent product id from Simple Product informations.

  7. I got what you convey, thanks for putting up. Woh I am pleased to hit this website through google. Thanks For Share How to get parent product id from Simple Product.

  8. You really make it appear really easy with your presentation however I find this matter to be really something which I think I’d by no means understand. It sort of feels too complex and extremely huge for me. I am looking forward in your next submit, I will attempt to get the hang of it!

  9. There are actually a whole lot of details like that to take into consideration. That could be a great level to convey up. I provide the thoughts above as normal inspiration however clearly there are questions like the one you deliver up where crucial thing will likely be working in honest good faith. I don?t know if best practices have emerged around issues like that, however I am sure that your job is clearly identified as a fair game.

  10. Great goods from you, man. How to get parent product id from Simple Product I have understand your stuff previous to and you’re just too excellent. I actually like what you have acquired here, really like what you’re stating and the way in which you say it. You make it enjoyable and you still take care of to keep it sensible. I can not wait to read much more from you. This is actually a terrific How to get parent product id from Simple Product informations.

  11. I got what you wish, thanks for putting up. Woh I am gladsome to uncovering this website through google. Thanks For Share How to get parent product id from Simple Product.

  12. I know this is really boring and you are skipping to the next comment, but I just wanted to throw you a big thanks – you cleared up some things for me!

  13. Thanks for this, I have implemented example two to succesfully get the parent product, however when I call getProductUrl it doesn’t return the SEO friendly URL but the “index.php/catalog/product/view/id/…..” one. Any ideas why this might be or how I can get the SEO friendly URL for the product?

  14. I was recommended this web site by my cousin. You’re incredible! Thanks! your article about How to get parent product id from Simple Product Best Regards Craig Nick

  15. I got what you impart, thanks for putting up. Woh I am happy to grow this website through google. Thanks For Share

  16. I open that you should write much on this substance, it power not be a taboo someone but mostly group are not sufficiency to verbalise on much topics.

  17. Wow, How long have you been blogging for? you made blogging look easy. let alone the content!. Thanks For Your article, you made things simple again.

  18. I wish to show my love for your generosity supporting folks who should have assistance with this particular idea. Your personal dedication to getting the message all around turned out to be especially important and has surely allowed most people just like me to get to their dreams. Your amazing valuable key points implies much a person like me and much more to my mates. Warm regards; from each one of us.

  19. hey admin thanks for wonderful and straightforward understandable article i loved your web site internet site genuinely much bookmarked also

  20. I precisely desired to thank you so much again. I do not know the things I would have followed in the absence of the entire creative concepts contributed by you regarding such a industry. It truly was a very terrifying circumstance in my position, however , considering the very skilled mode you handled the issue took me to leap over joy. I will be thankful for your help as well as wish you are aware of a great job you have been carrying out educating other individuals through your web site. I am certain you have never met any of us.

  21. whoah this blog is magnificent i love reading your articles. Keep up the good work! You know, a lot of people are hunting around for this info, you can help them greatly.

  22. Very good publish. I just came across your site and wanted to say that We have really liked reading your site articles. Any way Soon we will be subscribing to your feed and that i hope you post again soon.

  23. I can’t genuinely support but appreciate your blog internet site, your web site is adorable and good

Comments are closed.