Magento Themes and Magento Extensions

Magento Product Collections

Share

Using product collections can be very beneficial to Magento developers. In order to understand  Magento Product Collections, you should have an understanding of what a Collection is, in addition to having an understanding of Magento Models.

In short, a Magento Model is similar to an object in the sense that it contains both data and methods. A Magento Collection is a Model type that contains other models. So when we use the terminology “product collection”, we’re basically talking about all of the products in any given situation.

To retrieve all the products of a particular category, we simply open the category Model and load the product collection of that category. It looks something like this:

// @categoryId - integer - would be the id of the category that we're loading the products for
$product_collection = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection();

If we wanted to return the ID’s of all the products in that particular category, it’d look something like this:

// Let's load the category Model and grab the product collection of that category
$product_collection = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection();

// Now let's loop through the product collection and print the ID of every product in the collection
foreach($product_collection as $product) {
  echo $product->getId() . '';
}

Now let’s say we wanted to return some additional information from our products…Product collections contain what I like to call “limited product models”. A limited product model only contains a products basic information such as the product sku, name, etc. If we wanted to retrieve additional information from the product, such as the product image, url, etc…we’d need to load the full product model in order access it all.

You’re probably asking yourself why does Magento do this? The answer is simple. Product Models are *huge* and contain an enormous amount of data. Products contain hundreds of attributes and values that are not always necessary when working with product collections.

In order to increase performance, Magento decided to make product collections contain limited product models in order to downsize memory usage, server workload, and MySQL queries…Although loading the full product models may not be too much of a burden when you only have a hand-full of products — imagine having hundreds or even thousands of products!

To load the full product model within the product collections, you would do something like this this:

// Let's load the category Model and grab the product collection of that category
$product_collection = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection();

// Now let's loop through the product collection and print the ID of every product in the collection
foreach($product_collection as $product) {
  // Get the product ID
  $product_id = $product->getId();

  // Load the full product model based on the product ID
  $full_product = Mage::getModel('catalog/product')->load($product_id);

  // Now that we loaded the full product model, let's access all of it's data
  // Let's get the Product Name
  $product_name = $full_product->getName();

  // Let's get the Product URL path
  $product_url = $full_product->getProductUrl();

  // Let's get the Product Image URL
  $product_image_url = $full_product->getImageUrl();

  // Let's print the product information we gathered and continue onto the next one
  echo'<a href="'.$product_url.'">'.$product_name.'</a>';
  echo '<img src="'.$product_image_url.'"/>';
}

You just displayed all of the products (name, image, and link) of a particular category.

Obviously you can return more information from the full product, but this is just a basic tutorial to teach you how it all works.

Comments are closed.