当前位置: 动力学知识库 > 问答 > 编程问答 >

magento - Get parent SKU (configurable or bundle) from child SKU

问题描述:

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: 1.4.2.0

网友答案:

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:

Mage::getModel('catalog/product')->getIdBySku($sku);

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');
分享给朋友:
您可能感兴趣的文章:
随机阅读: