Magento Layered Nav PHP - Remove the filter in URL

Magento Layered Nav PHP - Remove the filter in URL

September 18, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Layered Nav PHP - Remove the filter in URL

Removing Magento Layered Navigation Filters from URL Using PHP

Magento’s layered navigation is a powerful feature that allows customers to filter products based on various attributes like price, color, size, etc. By default, it uses the ‘currently shopping’ section to display applied filters. However, you can manipulate this system to use checkboxes instead.

Setting Up Checkbox Filters

You’ve already managed to set up the checkbox system using the following PHP code:

<input type="checkbox" id="filter-checkbox-<?php echo $_item->getLabel() ?>" class="filter-checkbox" onclick="setLocation('<?php echo $this->urlEscape($_item->getUrl()) ?>');"/>

<a href="<?php echo $this->urlEscape($_item->getUrl()) ?>" class="m-notselected-ln-item" title="<?php echo $_item->getLabel() ?>">
  <?php echo $_item->getLabel() ?>
</a>

This code generates a checkbox and an anchor tag for each filter item. When the checkbox is clicked (checked), it adds the corresponding filter to the URL using JavaScript’s setLocation function.

The Challenge: Removing Applied Filters

Your current challenge lies in removing applied filters when the user unchecks a box. You’ve found that using $_filter->getRemoveUrl() breaks the page and affects the rest of the layered navigation.

A Practical Solution: Manipulating URLs Directly

To remove a filter from the URL, you’ll need to manually manipulate the current URL string. Here’s a step-by-step solution:

  1. Retrieve the current URL using PHP.
  2. Parse the query strings into an associative array.
  3. Remove the unwanted ‘filtered_attribute’ parameter(s).
  4. Reconstruct the URL.

Here’s how you can do this in your JavaScript setLocation function:

// Assuming $_item is a filter item and $_filter is the current filters collection

var url = new URL(window.location.href);
var params = new URLSearchParams(url.search);

// Get all 'filtered_attribute' parameters
var filteredAttributes = Array.from(params).filter(item => item[0].startsWith('filtered_attribute'));

// If the checkbox is unchecked (i.e., the filter should be removed)
if (!document.getElementById(`filter-checkbox-${$_item->getLabel()}`).checked) {
  // Remove the corresponding 'filtered_attribute' parameter
  filteredAttributes.forEach(item => params.delete(item[0]));

  // Update the URL without the removed filter
  url.search = params.toString();
}

// Redirect to the updated URL
window.location.href = url.toString();

This JavaScript code retrieves the current URL, extracts all ‘filtered_attribute’ parameters, and removes them when the corresponding checkbox is unchecked. It then updates the URL and redirects the user accordingly.

Best Practices

  • Remember to validate and sanitize user input to prevent any potential security vulnerabilities.
  • Ensure your URLs are SEO-friendly by using Magento’s built-in URL rewriting system once you’ve implemented this functionality successfully.
  • Clean up your code by removing duplicate elements (checkbox / anchor tag) and optimizing the structure.

Conclusion

Manipulating Magento’s layered navigation requires a good understanding of both PHP and JavaScript. By directly handling URLs in the client-side script, you can effectively add and remove filters without relying on Magento’s standard remove URL function, which may cause issues with other elements on the page.

Don’t forget to consult Laravel Company’s guide to Magento development for more in-depth insights into working with this e-commerce platform.

Catalog API Configuration Customers

Our network