Home » Magento

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

23 November 2009 13,517 views Popularity: 27% Share/Bookmark

email

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.

$_product = Mage::getModel('catalog/product')->load(YOUR_SIMPLE_PRODUCT_ID);
$parentIdArray = $_product->loadParentProductIds()->getData('parent_product_ids');
print_r($parentIdArray);

Related posts:

  1. Magento: Get order option of configurable and bundle product in cart
  2. Magento: How to get all associated children product of a configurable product?
  3. Magento: Get Product Collection by Type
  4. Magento: Fatal error: Call to a member function getTable() on a non-object
  5. Magento: Load store specific product
  6. Magento: How to filter product collection using 2 or more category filters?
  7. Magento: Product Edit Warning: Invalid argument supplied for foreach()
  8. Magento: Get category name and url from product
  9. Magento: Product is still visible in catalog and search even after changing visibility to nowhere
  10. Magento: Get manufacturer name and id from product
  • 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

  • http://blog.chapagain.com.np Mukesh

    @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

  • http://blog.chapagain.com.np Mukesh

    @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?

  • http://flale.com Bractar

    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

  • http://www.facebook.com/people/Ashley-Messer/100002081662385 Ashley Messer

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