Product Collection

In this blog, 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. Lets look at the important functions

Get All Products of a category
$collection = Mage::getResourceModel('catalog/product_collection')
            ->setStoreId($this->getStoreId())
            ->addCategoryFilter($category);

Tn this the addCategoryFilter() function, is used to get all products of a particular category. So, if you want to get all products of a certain category use this function.

Visibility Filter
$collection = Mage::getResourceModel('catalog/product_collection');
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);

The addVisibleFilterToCollection() adds visibility filter to a product collection i.e only products which are visible in frontend. The product which have “Not Visible Individually” selected in admin are removed.

Status Filter
$collection = Mage::getResourceModel('catalog/product_collection');
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);

This basically filters out products which are “Disabled”. Only “Enabled” products remain in the collection.

Add Product Price To Collection
$collection = Mage::getResourceModel('catalog/product_collection');
$collection ->addMinimalPrice()
            ->addFinalPrice()
            ->addTaxPercents();

This adds the product prices, i.e base price, final price etc to the collection. Also, price after tax, if applicable.

Filter By Ids
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addIdFilter(array(1,2,3));
//$collection->addIdFilter(array(1,2,3),false);

This puts an id filter, only product with ids 1,2,3 remain in the collection. The function parameter is true/false, this means include/exclude products from collection.

Add Website ID to the collection
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addWebsiteNamesToResult();

This adds website_id of each product to that collection. Only useful when using multiple websites in magento.

Filter Current Store Products
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addStoreFilter();
Filter Current Website Products
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addWebsiteFilter();
Get All Products Ids
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->getAllIds();

This returns an array with only products ids of collection.

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

This adds SEO friends urls to our product collection.

Add Category Ids
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addCategoryIds();

This will add category ids to the products.

Add Tier Pricing
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addTierPriceData();

This added tier pricing data to each product in the collection.While we are on this subject, let look at some important function of the Product Object as well.

Function in Product Object

getTypeInstance()

This is an important function in magento and used widely. This function return an object of product type class located in folder Mage_Catalog_Model_Product_Type. These classes have function specially related to their product type. For example, functions related to configurable product only are located at Mage_Catalog_Model_Product_Type_Configurable.

getIdBySku()

This functions returns the id of a product based on its sku. This is usually used, when you want to load a product, but you only have its sku.

There are many more functions in both class which you should go through and are very useful. These function saves a lot of time, than to write complex sql query.
  • Leo Leung

    how can i filter by product attribute?

    • $pcollection = Mage::getModel(‘catalog/product’)->getCollection()->addAttributeToSelect(‘*’)->addAttributeToFilter(‘phonetype’, array(‘eq’=> $attributeValue));

  • Bikash ranjan

    Thanks @manish ,i really impressed to see your blog i will keep in mind always to read:). actually i`m new in magento i want to progress my skill in magento .can you please provide the tips to improve myself. i will be appreciate if any of the help from your side:).

    Thanks
    again @MANISH

  • rakesh

    Nice description…..

  • Vishnu

    @Manish: I want to find item quantity of each items defined in bundle product, but don’t know how to do it. plz help

    • Manish Prakash

      Yes you can do what you have to do exactly in this load it by bundle product id and then see below how its working:

      $bundled_product = new Mage_Catalog_Model_Product();
      $bundled_product->load(ID);

      $selectionCollection = $bundled_product->getTypeInstance(true)->getSelectionsCollection(
      $bundled_product->getTypeInstance(true)->getOptionsIds($bundled_product), $bundled_product
      );

      $bundled_items = array();
      foreach($selectionCollection as $option)
      {
      $bundled_items[ ] = $option->product_id;
      }
      Now you will get each items in bundled products now just check that stock of each product. Hope this will help you 🙂