Displaying Multiple Level Menu with Product List in Magento
As a developer specializing in e-commerce platforms like Magento, I often encounter requests for complex menu systems that showcase categories, sub-categories, and products simultaneously. In this post, we’ll walk you through the process of creating a nested drop-down menu similar to the one found at Asia Connect’s Magento Store but with improved performance and clarity for your products.
Understanding the Current Challenge
The challenge lies in displaying a category (like ‘Cars’) with its sub-categories (‘New’ and ‘Used’), while also listing all associated products directly below each sub-category. The provided code attempts to achieve this through a rollover effect, but it falls short of actually listing the products as required.
Our Approach: A Cleaner Solution
To solve this issue, we’ll use Magento’s built-in capabilities for category and product list display. This method ensures better performance and maintainability compared to custom JavaScript solutions.
Step 1: Prepare Your Category Structure
Ensure your categories are set up correctly in the Magento admin panel:
- Create a root category called ‘Cars’.
- Inside ‘Cars’, create two sub-categories: ‘New’ and ‘Used’.
Assign all relevant products to these sub-categories using the Product > Categories option during product creation or editing.
Step 2: Customize Your Menu Template
Open your theme’s catalog/navigation/top.phtml file. This is where the top menu is rendered by default in Magento.
Step 3: Implementing the Nested Menu
We’ll use the getChildrenCategories() method to fetch sub-categories and the getCategoryCollection() with appropriate filters to display products.
<?php $_currentCat = $this->catalogSession->getCurrentCategory(); ?>
<?php if (count($_category->getChildrenCategories())): ?>
<ul class="menu">
<?php foreach ($_category->getChildrenCategories() as $_child): ?>
<li>
<a href="<?php echo $this->getCategoryUrl($_child) ?>">
<?php echo $_child->getName() ?>
</a>
<!-- Display products for this sub-category -->
<?php
$products = Mage::getModel('catalog/category')->load($_child->getId())
->getProductCollection()
->addAttributeToSelect('*');
if ($products->count()) {
echo '<ul class="sub-products">';
foreach ($products as $_product) {
echo '<li><a href="' . $this->getProductUrl($_product) . '">' . $_product->getName() . '</a></li>';
}
echo '</ul>';
}
?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Step 4: Compile Your CSS
Make sure your theme’s CSS is updated to style the new sub-product lists as desired. Update your css folder or use a preprocessor like LESS for easier management.
Conclusion
By following this approach, you’ll be able to display multiple levels of categories and their associated products in a clean, efficient manner using core Magento features. This method ensures better performance compared to heavy JavaScript solutions and provides a more maintainable codebase.
Remember to always backup your files before making changes and test thoroughly on a staging environment during development to ensure compatibility with your specific Magento setup.
For detailed guides on customizing Magento menus and themes, I recommend exploring the resources at LaravelCompany’s Magento Development Hub. They provide comprehensive tutorials and best practices for a wide range of e-commerce projects.