magento advanced catalog product search filter by categories

magento advanced catalog product search filter by categories

October 5, 2026 · Illustrated author avatarBy Stefan Bogdanescu
magento advanced catalog product search filter by categories

Advanced Catalog Search by Category Filter in Magento

As an experienced developer specializing in e-commerce platforms like Magento, I often encounter scenarios where clients need to implement advanced search functionality that filters products based on categories and sub-categories. In this post, we’ll explore the best approach to achieve this using Magento’s built-in features.

Understanding the Current Implementation

You’re currently working with a product collection generated by getProductCollection() method in your custom module. This code snippet already includes several important steps:

  1. Loads the advanced search product collection.
  2. Adds relevant attributes, price range, and store filter to the collection.
  3. Applies filters for visible products based on status and visibility settings.

However, you’ve commented out the line that adds category filtering to the collection, which is causing the issue when trying to include multiple categories as a filter:

if ($category = filter_input(INPUT_GET, 'category', FILTER_VALIDATE_INT)) {
    $this->productCollection->addCategoryFilter(Mage::getModel('catalog/category')->load($category), true);
}

Filtering Products by Categories and Sub-Categories

To search for products in any of the specified categories or their sub-categories, we need to modify the category filter process. Instead of applying a single category filter with addCategoryFilter(), we’ll use addAttributeToFilter() method with the category_ids attribute.

Here’s how you can achieve this:

/**
 * Retrieve advanced search product collection
 *
 * @return Mage_CatalogSearch_Model_Mysql4_Advanced_Collection
 */
public function getProductCollection(){
    if (is_null($this->productCollection)) {
        $this->productCollection = Mage::getResourceModel('catalogsearch/advanced_collection')
            ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
            ->addMinimalPrice()
            ->addStoreFilter();
        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($this->productCollection);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($this->productCollection);

        // Filter by categories and sub-categories
        $categoryIds = explode(',', filter_input(INPUT_GET, 'categories', FILTER_DEFAULT));
        if (!empty($categoryIds)) {
            $this->productCollection->addAttributeToFilter('category_ids', array('in' => $categoryIds));
        }
    }
    return $this->productCollection;
}

In this updated code:

  1. We extract the category IDs from the categories GET parameter using filter_input() with FILTER_DEFAULT.
  2. If any category IDs are provided, we use addAttributeToFilter() to filter the products by the specified categories and their sub-categories.

Conclusion

Implementing advanced search functionality in Magento requires a strategic approach to leverage built-in features effectively. By modifying the product collection generation process as shown above, you can now filter products based on multiple categories and their respective sub-categories without any additional complexity.

Remember that proper testing is essential for ensuring your custom implementation works seamlessly with your existing Magento setup. If you need further assistance or want to explore more advanced search features like faceted search or Elasticsearch integration, feel free to reach out to our team at Laravel Company.

Happy coding!

Catalog Extensions Configuration Pricing

Our network