5

i am looking for code which can help me get parent product id from child product Id and this both products are related using upsell feature

so far i was able to retrieve child products id from parent id by using below code

   //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(); 

but i want to do reverse

This solution also worked

$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();
}
Deepak Mallah
  • 1,649
  • 2
  • 18
  • 25

4 Answers4

3

Quick and dirty way of doing it:

$productId = 1;
$collection = Mage::getModel('catalog/product')->getCollection();
//$collection->addAttributeToSelect('*');//if you want all the attributes
$select = $collection->getSelect();
$joinCondition = array(
    'links.linked_product_id = e.entity_id',
    'links.link_type_id = '. Mage_Catalog_Model_Product_Link::LINK_TYPE_UPSELL
);
$select->join(
    array('links' => $collection->getTable('catalog/product_link')),
    implode(' AND ', $joinCondition),
    array('link_id')
);
$select->where('links.linked_product_id = ?', (int)$productId);
foreach ($collection as $item){
    //do something with $item
}
Marius
  • 197,939
  • 53
  • 422
  • 830
2

The following code will:

  1. Get the link instance of the current product,
  2. Filter based on the linked_product_id column on the table catalog_product_link,
  3. The call to useUpSellLinks will make sure only upselling links are returned,
  4. Give you a collection of all links where this product is set in the linked_product_id,

You can then loop through the collection and get the product_id attribute which will be the parent.

//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.
$_linkInstance = $_product->getLinkInstance();
/* @var $collection Mage_Catalog_Model_Resource_Product_Link_Collection */
$collection = $_linkInstance->useUpSellLinks()->getLinkCollection();
$collection->addFieldToFilter('linked_product_id',  array('eq' => $_product->getId()));
$collection->addLinkTypeIdFilter();
$collection->joinAttributes();

foreach($collection as $upsellproduct) {
    $upsellproduct->getProductId();
}
David Manners
  • 27,241
  • 9
  • 76
  • 220
1

If you want to get parent (configurable) product of a child product on product listing or product view page template in magento, you can achieve this by using 1 line code as below :

if (empty(Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($product->getId()))
echo $product->getName();

Reference : http://magentomonsters.com/get-parent-product-of-child-product-in-magento/

Arslan Hashim
  • 79
  • 1
  • 1
  • Be careful when using empty(), you'd need php 5.5 or higher to use it with some different than a variable http://php.net/manual/es/function.empty.php – Raul Sanchez Feb 17 '16 at 11:31
1

For related, upsell, crossell products also possible to use

$parentIds = $product->getLinkInstance()->getResource()
    ->getParentIdsByChild($product->getId(), Mage_Catalog_Model_Product_Link::LINK_TYPE_RELATED);
Alexander
  • 11
  • 1