Friday 17 May 2013

Get All Product Collection in magento

Get All Product Collection
 
<?php
       $product_collection = Mage::getModel('catalog/product')->getCollection()->setOrder('name', 'asc'); 
      
       //getting the product collection, results are ordered by product name
       
      foreach($product_collection as $_product)
       {
           echo $_product->getId() . "<br />";
       }
?>


Get Limitation product


       <?php
       $limit = 5;
       $starting_from = 0;
       $product_collection = Mage::getModel('catalog/product')->getCollection()->setOrder('name', 'asc'); 
       //getting the product collection, results are ordered by product name

       $product_collection->getSelect()->limit($limit,$starting_from);   
       
       //where $limit will be the number of results we want, $starting_from will be the index of the result set to be considered as starting point (note 0 is the index of first row)
       //this will ouptput 5 records starting from 1rd record

       foreach($product_collection as $_product)
       {
           echo $_product->getName().'<br/>';
           echo $_product->getId() . "<br />";
       }
       ?>


Get Latest Product Collection


       <?php
          $_productCollection = Mage::getResourceModel('reports/product_collection')
                           ->addAttributeToSelect('*')
                           ->setOrder('created_at', 'desc')
                           ->setPage(1, 5);
       ?>
       <h2>Latest Products</h2>
       <?php foreach($_productCollection as $_product) : ?>
        <a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?></a></br>
       <?php endforeach; ?>


5 comments:

  1. Hi,

    I got products from particular category in home page but it is limited by the sort per page count from list page...How can i get my all products from that category???

    Thanks in advance.,

    ReplyDelete
    Replies
    1. Hello Nithya,

      Replace $product_collection to below code.

      $category_id = 3;
      $product_collection = Mage::getModel('catalog/category')->load($category_id)
      ->getProductCollection()
      ->addAttributeToSelect('*') // add all attributes - optional
      ->addAttributeToFilter('status', 1) // enabled
      ->addAttributeToFilter('visibility', 4) //visibility in catalog,search
      ->setOrder('price', 'ASC'); //sets the order by price

      Try this code.

      Thanks,
      Kruti

      Delete
  2. Hi,
    can you suggest the code for top selling products in magento, i unable to get top selling products in magento please help me.
    Thanks
    gopinadh

    ReplyDelete
  3. Hi,
    Thanks for your code, working for me

    ReplyDelete
  4. The blog was absolutely fantastic! Lot of great information which can be helpful in some or the other way. Keep updating the blog, looking forward for more contents...Great job, keep it up..
    magento development company in bangalore 

    ReplyDelete

Thanks for visit blog.