Product Collection in Magento

Magento product collection are the very important, by these function we can saves a lot of time, than to write complex sql query. In this blog post, we will see some important function in magento product collection class. Product Collection class in magento is Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection which returns all the data into the raw manners. He we will discuss the important functions

1. General Product Collection:

$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addAttributeToFilter('status',1); //only enabled product
$collection->addAttributeToFilter('visibility',array("neq"=>1)); //except not visible individually
$collection->addAttributeToSelect(array('name','url','small_image')); //add product attribute to be fetched
//$collection->getSelect()->order('rand()');   //uncomment for getting the products in random order
foreach ($collection as $_product):
echo $_product->getName();   //get product name
echo 'There is no products';

2. Get All Products of a category or Product collection filter by the category

Suppose you want to get all the products of a certain category, addCategoryFilter() function, is used to get all products of a particular category. Syntax is given below:

$collection = Mage::getResourceModel('catalog/product_collection')

3. Add Product Price To Product Collection

Suppose you want to filter the products collection by the product prices, i.e base price, final price, price after tax, if applicable you can use the syntax given below:

$collection = Mage::getResourceModel('catalog/product_collection');
$collection ->addFinalPrice()

4. Add Tier Pricing in the Product Collection

Suppose you want to add the tier prices of the each products, Syntax is given below:

$collection = Mage::getResourceModel('catalog/product_collection');

5. Add Website ID to the Product Collection

Suppose you want to display the website ID with the store then it is necessary to know the website ID, It is useful when using multiple websites in magento, then use the syntax given below:

$collection = Mage::getResourceModel('catalog/product_collection');

6. Visibility Filter in Product Collection

It is useful when you want to display the only products which are visible in frontend. The product which have “Not Visible Individually” selected in admin will not display. The function addVisibleFilterToCollection() adds visibility filter to a product collection the syntax is given below:

$collection = Mage::getResourceModel('catalog/product_collection');

7. Status Filter in Product Collection

For Only to display the Enabled products Disabled products will removed from the Product Collection

$collection = Mage::getResourceModel('catalog/product_collection');

8. Product Collection Filter By Ids

$collection = Mage::getResourceModel('catalog/product_collection');

This will returns only product with ids 95,20,338.

9. Get All Products Ids

This will returns all the products ID

$collection = Mage::getResourceModel('catalog/product_collection');

10. Add Category Ids in Product Collection

$collection = Mage::getResourceModel('catalog/product_collection');

11. Add SEO Product URL in Product Collection

$collection = Mage::getResourceModel('catalog/product_collection');

Hope this will be helpful, Enjoy the Magento coding.