Magento: text field type attribute in layered navigation

Magento: text field type attribute in layered navigation

September 18, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: text field type attribute in layered navigation

Magento: Enabling Text Field Attribute Filtration in Layered Navigation

Are you facing difficulties using text field attributes for layered navigation in your Magento store? You’re not alone. By default, Magento only allows select (dropdown), multi-select, and price type attributes to be used in the layered navigation filter. However, there are ways to enable this functionality for text fields as well.

Why Text Fields Matter in Layered Navigation

In certain scenarios, you might need more than just dropdown or numeric filters. For instance, if your store sells books, you might want a “total pages” attribute that allows customers to filter products by the number of pages. A text field is perfect for this scenario, as it provides flexibility.

Understanding the Magento Limitations

Magento’s layered navigation module, out of the box, doesn’t support text fields because they don’t have a fixed set of options like dropdown or multi-select attributes do. This lack of structure makes filtering challenging to implement.

Enabling Text Field Filtration: A Step-by-Step Guide

To enable text field filtration in your Magento store, you’ll need to make some changes to the core files and create an observer. Here’s a step-by-step guide:

  1. Create an Attribute Set

    Navigate to Catalog > Attributes > Manage Attributes. Click on “Add New Attribute” and set the following properties:

    • Attribute Code: total_pages
    • Frontend Input: Text Field
    • Is Filterable (with results in layered navigation): Yes
  2. Override the Layered Navigation Block

    In your theme’s template folder, locate catalog/layer/view.phtml and add the following code inside the <div class="filter-nav"> tag:

    <?php if($_attribute = $this->getLayer()->getFilterableAttributes()): ?>
      <?php foreach ($_attribute as $_attribute): ?>
        <h4><?php echo $this->escapeHtml($_attribute->getStoreLabel()) ?></h4>
        <div class="filter-nav-content">
          <?php if (isset($_attribute->getData['options']) && count($_attribute->getData('options')) > 0): ?>
            <?php $_options = $_attribute->getData('options'); ?>
            <select name="<?php echo $this->escapeHtml($_attribute->getAttributeCode()) ?>" id="<?php echo $this->escapeHtml($_attribute->getAttributeCode()) ?>"
                    class="filter-field select" onchange="setLocation(this)" style="display: none;">
              <?php foreach ($_options as $_option): ?>
                <option
                      value="<?php echo $_option['value'] ?>"
                      <?php if($_option['count'] == 0){?> disabled="disabled"<?php }?>
                      <?php if($_request->getParam($this->escapeHtml($_attribute->getAttributeCode())) == $_option['value']): ?>
                        selected="selected"
                      <?php endif; ?>
                >
                  <?php echo $this->escapeHtml($_option['label']) ?> (<?php echo $this->escapeHtml($_option['count']) ?>)
                </option>
              <?php endforeach; ?>
            </select>
          <?php else: ?>
            <input type="text" name="<?php echo $this->escapeHtml($_attribute->getAttributeCode()) ?>" id="<?php echo $this->escapeHtml($_attribute->getAttributeCode()) ?>" class="filter-field input-text" onkeyup="setLocation(this)" value="<?php echo $this->escapeHtml($_request->getParam($this->escapeHtml($_attribute->getAttributeCode()))) ?>">
          <?php endif; ?>
        </div>
      <?php endforeach; ?>
    <?php endif; ?>
    
  3. Create an Observer

    Open your terminal and run the following commands to create a new file:

    mkdir -p app/code/local/YourCompany/LayeredNavigation/etc
    touch app/code/local/YourCompany/LayeredNavigation/etc/config.xml
    

    In config.xml, add the following content:

    <?xml version="1.0" encoding="UTF-8"?>
    <config>
      <modules>
        <YourCompany_LayeredNavigation>
          <version>1.0</version>
        </YourCompany_LayeredNavigation>
      </modules>
      <global>
        <events>
          <catalog_layer_filterable_attributes>
            <observers>
              <yourcompany_layerednavigation_add_text_field>
                <type>singleton</type>
                <class>YourCompany_LayeredNavigation_Model_Observer</class>
                <method>addTextFieldAttribute</method>
              </yourcompany_layerednavigation_add_text_field>
            </observers>
          </catalog_layer_filterable_attributes>
        </events>
      </global>
    </config>
    
  4. Create the Observer Class

    In your terminal, create a new class file:

    mkdir -p app/code/local/YourCompany/LayeredNavigation/Model
    touch app/code/local/YourCompany/LayeredNavigation/Model/Observer.php
    

    Add the following content to Observer.php:

    <?php
    class YourCompany_LayeredNavigation_Model_Observer
    {
      public function addTextFieldAttribute($observer)
      {
        $layer = $observer->getLayer();
        if ($layer instanceof Mage_Catalog_Model_Layer) {
          foreach ($layer->getFilterableAttributes() as $attribute) {
            if ($this->_isTextField($attribute)) {
              $layer->addFilterableAttribute($attribute);
            }
          }
        }
      }
    
      private function _isTextField($attribute)
      {
        return in_array($attribute->getFrontendInput(), array('text'));
      }
    }
    

Conclusion

Enabling text field filtration in Magento’s layered navigation is a straightforward process that requires some core file overrides and an observer. By following this guide, you can provide your customers with more flexible filtering options without limiting them to pre-defined dropdown choices.

Remember to always backup your files before making any changes. Also, consider using child themes for these modifications to ensure they don’t get overwritten during updates.

For a comprehensive guide on optimizing your Magento store’s performance and security, visit Laravel Company.

Happy coding!

Catalog Extensions Customers Pricing

Our network