Magento: Selecting Products Where Any of Multiple Attributes Is Not Empty
When working with Magento collections, you often need to filter products based on multiple attributes. However, Magento’s default filters work for single conditions only. This tutorial will guide you through selecting all simple products where any of the ean, upc, or jan attributes is not empty.
Understanding the Issue
The provided code snippet selects all simple products where the ean attribute is not empty. However, if a product has no ean but has either an upc or jan, it’s currently excluded from the collection.
To solve this, we need to create a custom filter that checks for the existence of any of the specified attributes. Let’s dive into the solution.
Creating a Custom Attribute Filter
Magento allows you to extend its functionality through plugins and observers. We’ll use a plugin to modify the addFieldToFilter method of the Mage_Core_Model_Resource_Db_Collection_Abstract class.
First, create a new file in your custom module: app/code/local/Your_Namespace/Your_Module/etc/config.xml.
Add the following configuration:
<config>
<modules>
<Your_Namespace_Your_Module>
<version>1.0</version>
</Your_Namespace_Your_Module>
</modules>
<global>
<models>
<yourmodule>
<class>Your_Namespace_Your_Module_Model</class>
</yourmodule>
<resources>
<your_module_setup>
<setup>
<module>Your_Namespace_Your_Module</module>
</setup>
</your_module_setup>
</resources>
</models>
</global>
</config>
Next, create a new file in app/code/local/Your_Namespace/Your_Module/etc/events.xml:
<config>
<events>
<add_field_to_filter>
<observers>
<yourmodule_add_field_to_filter>
<type>model</type>
<class>yourmodule/event_observer</class>
<method>addAttributeFilter</method>
</yourmodule_add_field_to_filter>
</observers>
</add_field_to_filter>
</events>
</config>
Finally, create the observer file at app/code/local/Your_Namespace/Your_Module/Model/EventObserver.php:
class Your_Namespace_Your_Module_Model_EventObserver
{
public function addAttributeFilter($observer)
{
$collection = $observer->getCollection();
$filters = array(
'ean' => array('neq' => ''),
'upc' => array('neq' => ''),
'jan' => array('neq' => ''),
);
foreach ($filters as $attributeCode => $filterData) {
if (!empty($filterData)) {
$collection->addAttributeToFilter($attributeCode, $filterData);
}
}
return $this;
}
}
This observer will be triggered whenever the addFieldToFilter method is called on a collection. It checks for non-empty values in multiple attributes and applies them as filters to the collection.
Using the Custom Filter
To use this custom filter, modify your original code:
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('jan')
->addAttributeToSelect('name')
->addAttributeToSelect('upc')
->addAttributeToSelect('ean')
->addAttributeToSelect('price')
->addFieldToFilter(array(
array('attribute' => 'type_id', 'eq' => 'simple')
))
->setPageSize(5);
Now, the collection will include all simple products where at least one of ean, upc, or jan is not empty.
Conclusion
In this tutorial, we’ve demonstrated how to create a custom filter in Magento that checks for non-empty values across multiple attributes. By using plugins and observers, you can extend Magento’s built-in functionality to meet your specific needs.
Remember to clear the cache after making these changes. You can do so manually or use a tool like Laravel Company’s Cache Management extension for easier cache management.
The key takeaway is that understanding Magento’s event-driven architecture allows you to create powerful, flexible solutions tailored to your business requirements.