Filtering Simple and Configurable Products Together in Magento
As experienced developers and bloggers at Laravel Company, we often receive queries about customizing product filtering in Magento. One common challenge is to filter simple and configurable products based on the same attribute while displaying them together.
In this post, we will guide you through a practical solution for filtering simple and configurable products based on an attribute without separating them during or after the filtering process.
Understanding the Default Behavior
By default, when applying a filter in Magento, it checks if the product itself satisfies the filter condition. For example, if you create a configurable product with attributes like color (red) and size (small), and then apply a “color = red” filter, only simple products with “color = red” will be displayed.
Configurable products themselves are considered filtered out because their main product entity doesn’t match the filter condition directly. This behavior is built into Magento’s search and filter functionality to ensure accurate results.
The Solution: Combining Products During Filtering
To achieve your desired outcome, we need to modify the filtering process to consider both simple and configurable products as a single entity for the purpose of matching filters. Here are the steps:
-
Create an additional column in your product table (catalog_product_entity) that represents the combined attributes you want to filter by. For simplicity, let’s call this column “combined_attributes”.
-
Populate the ‘combined_attributes’ field for both simple and configurable products during product creation or update. You can use a script or cron job to generate this value based on your configuration:
<?php
$products = Mage::getModel('catalog/product')->getCollection();
foreach ($products as $product) {
if ($product->getTypeId() == 'configurable') {
// Fetch child products and combine attributes
$childProducts = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect(array('color', 'size')) // Specify the relevant attributes here
->addFieldToFilter('parent_id', $product->getId())
->load();
$combinedAttributes = '';
foreach ($childProducts as $child) {
$combinedAttributes .= $child->getColor() . '|' . $child->getSize() . ',';
}
$product->setCombinedAttributes(rtrim($combinedAttributes, ','));
} elseif ($product->getTypeId() == 'simple') {
// For simple products, use the direct attribute values
$product->setCombinedAttributes($product->getColor() . '|' . $product->getSize());
}
$product->save();
}
- Modify your filter code to search within this new “combined_attributes” column:
$filter = Mage::helper('catalogsearch')->getFilter('color');
$products = $filter->getResults($queryText);
- Display the filtered products in your template, ensuring that configurable products are treated like simple ones.
Conclusion
By following these steps and using our example code snippets, you can effectively filter both simple and configurable products together based on the specified attributes without separating them during or after the filtering process.
Remember to test this solution thoroughly across different scenarios, and consider optimizing your database queries for performance if dealing with a large number of products.