Magento Layered Navigation for Custom Product Collections
As a senior developer at LaravelCompany, I often encounter scenarios where clients need to display large product collections on custom pages with improved filtering options, like layered navigation and pagination. This blog post will guide you through the process of adding layered navigation to your custom Magento page with a specific product collection.
Understanding Your Current Setup
You’ve already created a module with a new controller and front-name for displaying particular product collections. To add layered navigation using the catalog/layer_view block:
<reference name="left">
<block type="catalog/layer_view" name="catalog.leftnav" template="landing/layer.phtml"/>
</reference>
However, this approach only applies the default layered navigation to your custom page. It doesn’t interact with the specific product collection you’re displaying on that page.
Creating a Custom Layered Navigation
To make the layered navigation work with your custom product collection, follow these steps:
- Create a new block: In your module, create a new XML layout file (e.g., catalog.xml) and add a new block for your specific layered navigation:
<reference name="root">
<block type="catalog/layer_view" name="custom_layered_nav" template="my_module/layer.phtml"/>
</reference>
-
Create the template: Create a new layer.phtml file in
app/design/frontend}/{your_theme}/{your_package}/template/my_module/to customize the layered navigation template. -
Customize the block in your controller:
In your module’s controller, you can customize the layered navigation by calling its setLayer() method:
$layer = Mage::getModel('catalog/layer');
$layer->setCurrentCategory(Mage::registry('current_category'));
Mage::register('_current_layout', $layer);
- Assign a new collection: To make sure your layered navigation works with the specific product collection, assign this collection to the layer object:
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addFieldToFilter('your_filter_field', array('eq' => 'your_value')); // Filter based on your needs
// Set the layered navigation's collection
$layer->setCollection($collection);
- Apply the layout: Finally, apply your custom layout in the controller:
$this->loadLayout();
Pagination and Sorting Options
To add pagination to your custom page, you can use the following method in your controller:
$pager = $this->getLayout()->createBlock('page/html_pager');
$pager->setCollection($collection);
$pagingBlock = $this->getLayout()->getBlock('page.html.pager');
if ($pagingBlock) {
$pagingBlock->setTemplate('page/html/pager.phtml'); // Adjust the template as needed
}
$this->setPager($pagingBlock);
For sorting options, you can add a custom sort order to your collection and provide filters for customers to sort by various attributes.
Conclusion
By following these steps, you should now have layered navigation that works specifically with your custom product collection on the new page. This approach allows you to display large collections more effectively by providing better filtering options.
Remember, proper customization of templates is essential to ensure a seamless integration with your theme’s design. For further reference and best practices in Magento development, visit LaravelCompany.
Don’t hesitate to reach out if you have any questions or need help with implementing this solution for your project. Our team at LaravelCompany would be happy to assist you!