在magento中查找产品的父ID

时间:2013-05-14 12:18:51

标签: magento

这是我的代码。

 $storeId = Mage::app()->getStore()->getId();
    $session = Mage::getSingleton('checkout/session');
    foreach($session->getQuote()->getAllItems() as $item) 
    {//how to find parent id here}

在上面提到的评论中,我想访问该特定产品的父ID。 请帮助..

3 个答案:

答案 0 :(得分:7)

尝试以下代码,可能会对您有所帮助

if($item->getTypeId() == "simple"){
    $parentIds = Mage::getModel('catalog/product_type_grouped')->getParentIdsByChild($item->getId()); // check for grouped product
    if(!$parentIds)
        $parentIds = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($item->getId()); //check for config product

}

答案 1 :(得分:2)

您可以使用它来获取父

foreach(...) {
    $parents = $item->getTypeInstance()->getParentIdsByChild($item->getId());
    //if you need to load the parent
    if(!empty($parents)) {
        $parentProd = Mage::getModel('catalog/product')->load($parents[0]);
        //do something
    }
}

答案 2 :(得分:0)

这可能会对您有所帮助:

$product = Mage::getModel('catalog/product');
$product->load($productId);
$grouped_product_model = Mage::getModel('catalog/product_type_grouped');
$groupedParentId = $grouped_product_model->getParentIdsByChild($product->getId());