Magento addAttributeToFilter, filter by attribute text

Magento addAttributeToFilter, filter by attribute text

July 28, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento addAttributeToFilter, filter by attribute text

Filtering Magento Products by Manufacturer Name

When working with the Magento e-commerce platform, you may find yourself needing to retrieve products based on various attributes such as manufacturer or brand names. However, Magento’s built-in filtering functions expect integer IDs rather than textual attribute values like “Nike”.

This guide will walk you through two common approaches to solve this challenge: using the getAttributeIdByCode method and utilizing the Magento API.

1. Using getAttributeIdByCode

The first method involves retrieving the manufacturer ID from its code (name). Here’s a step-by-step example:

// Retrieve the manufacturer ID by name
$manufacturerId = Mage::getModel('catalog/product')
    ->getResource()
    ->getAttribute('manufacturer')
    ->getSource()
    ->getOptionId('Nike');

// Now use this ID to filter products
$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToFilter('manufacturer', $manufacturerId);

In this code, we first retrieve the manufacturer attribute resource. We then get its source options and map the ‘Nike’ name to an ID using getOptionId. This ID is subsequently used in the product collection filter.

Remember to replace 'Nike' with your desired manufacturer name. Also, ensure that the manufacturer attribute is configured correctly in Magento’s admin panel.

2. Utilizing the Magento API

If you’re dealing with a more complex scenario or need greater flexibility, consider using the Magento API. You can use the REST API to search for products by attributes without needing to translate names into IDs manually.

To achieve this:

  1. Enable the Magento API if it isn’t already (System > Configuration > Services > Magento Core API).
  2. Generate an API key with appropriate permissions.
  3. Send a GET request to https://<your-magento-url>/rest/V1/products?searchCriteria[filter_groups][0][filters][0][field]=manufacturer&searchCriteria[filter_groups][0][filters][0][value]=Nike.

Here, replace <your-magento-url> with your actual Magento installation’s URL.

The API will return a JSON response containing products matching the specified manufacturer. You can handle this response in your application as needed.

Best Practices

When dealing with attribute filters:

  • Always validate user input to prevent SQL injection attacks.
  • Consider performance implications, especially for large catalogs. Use pagination or filtering efficiently.
  • If using the API, ensure proper security measures are in place to protect sensitive data.

Conclusion

Retrieving products by manufacturer name instead of ID requires some additional steps in Magento. The getAttributeIdByCode method provides a simple solution when you only need to filter within your application logic.

For more complex scenarios or external integrations, the Magento API offers greater flexibility and separation of concerns. It allows you to handle filtering requests outside your core Magento installation.

Remember that efficient attribute filtering is crucial for performance in e-commerce applications. Regular optimization and indexing can significantly improve search speeds as your catalog grows.

Check out our comprehensive guide on Magento Performance Optimization at LaravelCompany for more tips and best practices.

Happy coding!

Catalog API Admin Configuration

Our network