This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

CloudwaysCDN — a powerful solution that offers superior performance and satisfied global audience for your business. Read More

Use These Magento Product Collections Instead of SQL

Updated on  10th July

3 Min Read
Reading Time: 3 minutes

At some point, a Magento developer has to deal with Magento Model Collections. Magento collections represent an efficient way of grouping models together and it’s an easy option of grabbing a group of models from the database with almost little to no SQL code (thanks to its unique methods). Collections are tricky to work with, but they offer extensive data about products, customers, categories, attributes, etc.

Magento Product Collections

In this article, I will cover some useful methods of loading the Product Collection, adding Attributes to Collections, filtering and sorting Collection, etc.

Let’s dive into it.

Loading Product Collection in Magento

There are two ways of loading product collection:

Call getCollection() method in the product model instance:

Load collection class in the getResourceModel() factory method:

Cloudways Magento Hosting For Developers

Adding Attributes to a Collection in Magento

The product model is a huge set of data and calling more attributes in the collection usually takes a lot of time to load. By default, Magento only loads the simple data found in the catalog_product_entity table, which includes IDs, SKUs, Entity Type IDs, etc.

You can also check out how to configure Magento Attributes blog post.

Check out these convenient methods for adding further product attributes:

Add All Attributes

Add Individual Attributes

Add Multiple Attributes

Filtering Collections in Magento

Magento is an EAV system that gives access to the addAttributeToFilter() method which can take a larger range of arguments as compared to the general addFieldToFilter() filter method.

Equals To

Not Equals To

Greater Than

Less Than

Equals To OR Greater Than

Equals To OR Less Than

Is Null

Is Not Null

In Array

Not In Array

Does Contain (Including % Wildcard)

Does Not Contain (Including % Wildcard)

Sorting Collections in Magento

You can also sort a collection in Magento by attributes in ascending, descending or random order.

Ascending Order

Descending Order

Random Order

Limiting Collection Results in Magento

If you want to limit the product collection results to a certain number, use the following method. You only need to pass a number as an argument.

Debugging Collections in Magento

If you need to debug a collection and wish to know why it is continuously returning a particular set of results, use this debugging method and print the query that is hitting the database.


The Magento Model Collections essentially make data modeling and filtering very easy without passing any MySQL queries. The good thing is that the Magento platform handles this “magic” on its own without requiring third party extensions. In this article, I have just scratched the surface and there are still a number of functions which would greatly help you in Magento projects by saving a lot of time that you would otherwise waste on writing complex SQL queries.

If you need help, do leave a comment and I will get back to you ASAP.


Share your opinion in the comment section. COMMENT NOW

Fayyaz Khattak

Fayyaz is a Magento Community Manager at Cloudways - A Managed Magento Hosting Platform. His objective is to learn & share about PHP & Magento Development in Community. Fayyaz is a food lover and enjoys driving. You can email him at

Start Growing with Cloudways Today.

Our Clients Love us because we never compromise on these

Get Our Newsletter
Be the first to get the latest updates and tutorials.