addAttributeToFilter and OR condition in Magento's Collection

addAttributeToFilter and OR condition in Magento's Collection

July 3, 2026 · Illustrated author avatarBy Stefan Bogdanescu
addAttributeToFilter and OR condition in Magento's Collection

Building Magento Product Collections with Multiple Attribute Filters

As an experienced Magento developer, you often need to fetch products based on specific criteria for your custom modules or site features. While the addAttributeToFilter method is powerful for single attribute conditions, it can be challenging to apply OR logic across multiple attributes.

Let’s explore how to select products depending on several criteria from different attributes using an OR condition.

Understanding Magento’s Collection Filters

In Magento, product collections allow you to filter and retrieve specific sets of products based on various attributes. The addAttributeToFilter method enables you to apply conditions like equals, not equals, greater than, etc., on a single attribute.

For example, to fetch all products with the attribute ‘color’ set to ‘red’, you would use:

$collection->addAttributeToFilter('color', array('eq' => 'red'));

However, what if you want to find products where either the ‘color’ is ‘red’ OR the ‘size’ is ‘medium’? This requires an OR condition across multiple attributes.

Applying OR Conditions with Magento’s addFieldToFilter

While addAttributeToFilter primarily supports AND conditions, you can achieve OR logic by using a combination of addFieldToFilter and conditional operators within your where clause.

Here’s the approach to select products based on either ‘color’ or ‘size’:

$collection->getSelect()->where(
    '(e.color = ? OR e.size = ?)',
    array('red', 'medium')
);

In this example, we create a subquery using the addFieldToFilter method to fetch products where either ‘color’ equals ‘red’ or ‘size’ equals ‘medium’.

Retrieving Products Using Multiple Attribute Filters

Now let’s combine the OR condition with additional filters and sort options. Suppose you want to find all red (color) or medium (size) products in stock, sorted by name:

$collection = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToFilter(
        array(
            'color' => array('eq' => 'red'),
            'size'  => array('eq' => 'medium'),
            'status' => array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED),
        )
    )->addFieldToFilter(
        array(
            'stock_item',
            'sku',
            'is_in_stock'
        ),
        array(
            'gt' => 0,
        )
    );

$collection->setOrder('name', 'asc');

In this example, we first apply multiple attribute filters using addAttributeToFilter and then use addFieldToFilter to ensure the products are in stock.

Conclusion

Creating Magento product collections with OR conditions across multiple attributes requires utilizing the correct methods and constructing proper where clauses. By combining addAttributeToFilter for specific attribute filters and using subqueries within addFieldToFilter, you can achieve complex filtering logic seamlessly.

Remember to always test your collection queries thoroughly, as incorrect usage may lead to performance issues or inaccurate results in a production environment.

For more advanced Magento development tips and best practices, be sure to explore our comprehensive guides at LaravelCompany.com.

Catalog Extensions

Our network