Magento v1.4 / E1.9 Getting Parent Product from Simple Product

Home / Blog / Magento v1.4 / E1.9 Getting Parent Product from Simple Product

In previous versions of Magento, the approach I would take to get a product’s parent product would be the following:

$parentIdArray = $product->loadParentProductIds()->getData('parent_product_ids');
$parent = Mage::getModel('catalog/product')->load($parentIdArray['0']);

In the latest version of Magento, Mage_Catalog_Model_Resource_Eav_Mysql4_Product::getParentProductIds() has been removed. Its odd though because line 1338 of Mage_Catalog_Model_Product still references this missing method. So, if you use the above code to retrieve a product’s parent product, then it will blow up with a missing method exception.

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:

public 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);

With the above 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.

Showing 3 comments
  • Alex

    It works great!!!

  • brogr

    For Magento 1.4.2 and above use the following method instead:

    $configurable_product_model = Mage::getModel(‘catalog/product_type_configurable’);
    $parentIdArray = $configurable_product_model->getParentIdsByChild($simple_product_id);

  • tmillhouse

    Thanks for the suggestion!

Leave a Comment