Adding Category Links in Layered Navigation for Magento
One of the most common customizations requested by Magento store owners is to modify the layered navigation so that clicking on a category opens directly to the category page instead of filtering results on the same page. This can significantly improve user experience and navigation flow within your e-commerce store.
Let’s break down this modification into two main steps:
Step 1: Removing Category Filters
The first step is to remove the category filters from the layered navigation section entirely. To do this, you need to override the catalog/layer/view.phtml template file located in your current theme directory (usually under app/design/frontend/{Vendor}/{Theme}/template/catalog/layer).
Open the view.phtml file and locate the following lines of code:
<div class="block-layered-nav-block">
<?php echo $this->getStateHtml() ?>
</div>
Comment out or delete these lines. This will remove the category filter sections from your layered navigation.
Step 2: Adding Subcategory Links
Now, let’s add subcategory links to your layered navigation. To achieve this, you can use a static block and place it above the filters section using the Magento layout XML system.
First, create a new CMS page in your Magento admin panel (Content > Pages) with content being the HTML for your subcategory links. You can style these links as per your store’s theme.
Next, navigate to your Magento admin panel (Content > Widgets), click on “Add New Widget”, and select the following settings:
- Type: CMS Static Block
- Layout: Your category layout or a specific page layout where you want this widget to appear.
- Sort Order: Specify a number (e.g., 10) for the desired order of display.
Click “Save” to create the widget. Now, the subcategory links from your static block will appear above the layered navigation filters on all pages that have the specified layout applied.
Remember, if you want these changes to persist across theme updates or migrations, it’s recommended to move the modified files and layout XML to the app/code/{Vendor}/{Module}/view/frontend directory structure of a custom module. This way, your modifications won’t be lost during theme changes.
For more in-depth guidance on Magento theme development and customizations, I recommend checking out our comprehensive guide at https://laravelcompany.com/magento-theme-development-guide. It provides detailed walkthroughs of the theme structure, layout XML, and template overrides.
In conclusion, adding category links to your layered navigation for direct navigation can significantly enhance user experience. By following these steps and leveraging Magento’s built-in tools like CMS pages and widgets, you can accomplish this without needing complex core file modifications.