Home » Magento23 November 2009

Magento: Get parent id of simple product associated to configurable product

Scenario:

A simple product is associated with a configurable product. You have the id of the simple product. Now, you need the id of the configurable product with which the simple product is associated.

Get parent product id, i.e. get id of configurable product from a simple product.

Note: This method is deprecated after 1.4.2.0

This is the working method.

Hope it helps. Thanks.

Magento

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • saho

    Thanks for this.

    If you want to get any data from that parent I did it this way.

    $_product = Mage::getModel(‘catalog/product’)->load($productId);
    $parentIdArray = $_product->loadParentProductIds()->getData(‘parent_product_ids’);
    $parentSku = Mage::getModel(‘catalog/product’)->load($parentIdArray[‘0’])->getSku();
    echo $parentSku;

  • edo

    Hi, if i have parent product id how do u get the child id ? Please help me.

    p.s Love ur blog very useful

    Thanks

  • @edo:- You can easily get child products from parent product id.

    First load product by product id.
    $product = Mage::getModel('catalog/product')
    ->load(YOUR_PRODUCT_ID);

    You can get child products id (only ids) by:-
    $childIds = Mage::getModel('catalog/product_type_configurable')
    ->getChildrenIds($product->getId());

    You can get child products (all data) by:-
    $childProducts = Mage::getModel('catalog/product_type_configurable')
    ->getUsedProducts(null,$product);

    Hope this helps.

  • rumpa

    how to get child product id of bundle product in magento

  • rumpa

    how to get child product id of bundle product

  • Martin

    This is a really stupid question but could someone explain where to put this code??

    Thanks a lot in advance

  • riti

    Hi,

    I have configurable product id with me.

    Can you please tell me how to fetch data for config products?

    I am using

    $_product = new Mage_Catalog_Model_Product();
    $_product->load($_config_product_id);

    But it is giving me error
    Fatal error: Call to a member function getSource() on a non-object in app/code/core/Mage/Catalog/Model/Product.php on line 1150

    Can you please help.

    Regards,
    Riti

  • @riti –

    $product = Mage::getModel(‘catalog/product’)
    ->load(YOUR_CONFIGURABLE_PRODUCT_ID);

    Have a look at this link: http://blog.chapagain.com.np/magento-how-to-get-all-associated-children-product-of-a-configurable-product/

  • vishal lakhani

    hi mukesh, can you tell me how to get Category and parent category name from product id please help me!!!

  • Malik

    same problem as vishal got.
    any genius to solve this problem

  • Noctifer

    Hello, I’m working with Magento Entreprise 1.9 and I have this problem

    Fatal error: Call to undefined method Mage_Catalog_Model_Resource_Eav_Mysql4_Product::getParentProductIds() in /usr/local/apache2/htdocs/app/code/core/Mage/Catalog/Model/Product.php on line 1338

    Does somebody have a solution?

  • Hi,
    I’m also working with Magento Entreprise 1.9 and I had the same problem.
    Magento team removed the function from Mage_Catalog_Model_Resource_Eav_Mysql4_Product. I worked with Magento Entreprise 1.6 and it was working. So I just copied the function and now it works fine.
    Here is the function in case you need it.

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

    I’m opened to any other solution. There’s probably a reason why they removed this function.

  • pawdat

    I’ve found another way to get parent product ids if you have a child product Id:

    $_product = Mage::getModel(‘catalog/product’)->load(YOUR_PRODUCT_ID);
    $configurable_product = Mage::getModel(‘catalog/product_type_configurable’);
    $parentIdArray = $configurable_product->getParentIdsByChild($_product->getId());

    I wish it will help someone:D

  • How do you get the parent configurable product of a simple product, using the magento’s SOAP api?

  • Djbdfsgd

     Thanks ! Actually chapagains solution didn’t work, but yours did ! Great !

  • Its really a nice post, your sharing regarding configurable product saves my time.
    Thanks