On the cart page I need to be able to obtain the parent SKU using a child SKU.
I've tried several code snipped from both the Magento forums and similar questions here on StackOverflow without success.
I'm able to determine if a product is just a simple product without a parent by using getTypeId() but after that everything I try fails to result in getting at the parent SKU.
Magento Version: 22.214.171.124
Take a look at the Mage_Catalog_Model_Product_Type_Configurable and Mage_Bundle_Model_Product_Type classes. They have useful methods for getting parent and child products. You want getParentIdsByChild():
For configurable products:
$parent_ids = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($childId);
For bundle products:
$parent_ids = Mage::getModel('bundle/product_type')->getParentIdsByChild($childId);
These only work with ids. You'll need to convert the child SKU to an id and then the parent id back to a SKU. A simple way to get the id from the SKU is:
Also, you can have multiple parent ids, so you'll have to be aware of that. Here's an example:
$child_id = Mage::getModel('catalog/product')->getIdBySku($child_sku); $parent_ids = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($child_id); $parent_collection = Mage::getResourceModel('catalog/product_collection') ->addFieldToFilter('entity_id', array('in'=>$parent_ids)) ->addAttributeToSelect('sku'); $parent_skus = $parent_collection->getColumnValues('sku');