Monday, July 30, 2012

Magento:: Get Upsell products collection using Product Id


<?php
  
   // Get product object.
   $object = Mage::getModel('catalog/product');
  
   //Get product detail using product id  (Suppose you have product id is : $product_id)
   $_product = $object->load($product_id);
 
   // 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