Get Upsell products list using Product Id in Magento

Using Product ID you can get list of Upsell product. Copy following code and place it in you custom file.
<?php
   // Get product object.
   $object = Mage::getModel('catalog/product');
   
   /* Get product detail using product id   : $_product->getId() */
   $product = $object->load($_product->getId()); 
  
   // Fetch list of upsell product using query.
   $upsell_product = $product->getUpSellProductCollection()->addAttributeToSort('position', Varien_Db_Select::SQL_ASC)->addStoreFilter(); 

   //check if record is empty or not
   $count = count($upsell_product); 
   if(empty($count)) : 
       //if empty
       echo "Record not found";

   else:

     //if result is not empty then get  upsell product detail using foreach loop
      foreach($upsell_product as $_upsell):
         
         //get detail of single upsell prdocut using upsell product id
         $upsp = $object->load($_upsell->getId());

         echo "Product Name : ". $upsp->getName();
         echo "Poduct url : ". $upsp->getProductUrl();
         echo "Product regular price : ". $upsp->getPrice();
         
       endforeach;
   
   endif;

?>

No comments:

Post a Comment