追加销售:从子产品ID获取父产品

时间:2013-10-04 11:20:43

标签: magento

我正在寻找可以帮助我从子产品ID中获取父产品ID的代码,并且这两种产品都是使用追加销售功能相关的

到目前为止,我可以使用以下代码

从父ID中检索子产品ID
   //Get product detail using product id  (Suppose you have product id is : $product_id)
   $_product = Mage::getModel('catalog/product')->load($product_id);

   // Fetch list of upsell product using query.
   $upsell_product = $_product->getUpSellProductCollection(); 

但我希望以相反的方式结果

1 个答案:

答案 0 :(得分:3)

$childProductId = 17;//edit this value, or get it by $product->getId()
$productsLinkedAsUpsell = Mage::getModel('catalog/product_link')->getCollection()
        ->addFieldToFilter('linked_product_id', $childProductId)
        ->addFieldToFilter('link_type_id', Mage_Catalog_Model_Product_Link::LINK_TYPE_UPSELL);
foreach ($productsLinkedAsUpsell as $upsell) {
    $parentId = $upsell->getProductId();
}