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:

<?php
$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
$collection->addStoreFilter();
if(!empty($collection))
{
foreach ($collection as $_product):
echo $_product->getName();   //get product name
endforeach;
}else
{
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')
->setStoreId($this->getStoreId())
->addCategoryFilter($category);

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()
->addMinimalPrice()
->addTaxPercents();

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');
$collection->addTierPriceData();

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');
$collection->addWebsiteNamesToResult();

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');
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($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');
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);

8. Product Collection Filter By Ids

$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addIdFilter(array(95,20,338));

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');
$collection->getAllIds();

10. Add Category Ids in Product Collection

</pre>
$collection = Mage::getResourceModel('catalog/product_collection');
 $collection->addCategoryIds();

11. Add SEO Product URL in Product Collection


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

Hope this will be helpful, Enjoy the Magento coding.