Tuesday 7 May 2013

Magento : Get Upsell products list using Product Id .

In some situation when you need to show procuct in custom location file and in that file you also wanna show list of upsel product then this is example which will explain you.
For Example : Suppose you have Product ID and using that ID you want to accees upsell procust list then you can use following code :

1. Assume that you have product ID is : $product_id

2. Using that 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  (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