Moving Product Filters Above the List in Magento
As an experienced developer, I often receive queries from clients looking to customize their Magento stores to better suit their business needs. One common request is to relocate the product filters from the left sidebar to a position above the product list.
Let’s dive into how you can achieve this using PHP and Magento’s template system.
Why Your Approach Didn’t Work
Your initial attempt at moving the layer view template (view.phtml) code to the main product list template (list.phtml) didn’t work because of the way Magento’s template inheritance system functions. The list.phtml file is rendered inside a block that doesn’t include the necessary layout structures for the filters.
The Correct Approach: Override the Catalog Product List Block
To successfully move your product filters, you need to override the catalog product list block (catalog_product_list) and add the filter content there. Here’s how:
-
Create a
local.xmlfile in your theme directory (e.g.,app/design/frontend/yourtheme/default/layout/local.xml). -
Add the following XML code inside the
<layout>tags to override the catalog product list block:
<layout version="0.1.0">
<default>
<reference name="catalog_product_list">
<action method="setTemplate">
<template>yourtheme/catalog/product/list.phtml</template>
</action>
</reference>
</default>
</layout>
Replace yourtheme with the name of your theme.
-
Create a new file named
list.phtmlin your theme directory atapp/design/frontend/yourtheme/default/template/catalog/product/. -
Copy the contents of the default
list.phtmlfrom/template/catalog/product/into this new file, as it contains essential logic for rendering the product list. -
Add the following code above the line
<div class="category-products">:
<p class="block-subtitle"><?php echo $this->__('Shopping Options') ?></p>
<dl id="narrow-by-list">
<?php $_filters = $this->getFilters() ?>
<?php foreach ($_filters as $_filter): ?>
<?php if($_filter->getItemsCount()): ?>
<dt><?php echo $this->__($_filter->getName()) ?></dt>
<dd><?php echo $_filter->getHtml() ?></dd>
<?php endif; ?>
<?php endforeach; ?>
</dl>
This code retrieves the available filters and displays them in a definition list.
Conclusion
Relocating product filters above the list requires a proper understanding of Magento’s template inheritance system. By overriding the catalog product list block, you can add your custom template file that includes the filter content.
Remember to clear your cache after making these changes for the updates to take effect. You can do this manually by logging into the admin panel and navigating to System > Cache Management > Flush Magento Cache.
For more advanced customizations and best practices in Magento development, I recommend exploring the resources at Laravel Company. They provide comprehensive guides on Magento theme development, layout customization, and other essential topics for e-commerce developers.