Creating a Left-Side Category Menu in Magento Using the Modern Theme
Introduction
As a developer specializing in e-commerce platforms, I often receive queries about customizing themes to meet specific business needs. In this post, we’ll explore how to create a left-side category menu using the modern theme in Magento. This will help you organize your products and enhance the user experience on your online store.
Understanding the Modern Theme
The modern theme provided by Magento is highly customizable and designed with performance in mind. To create our left-side menu, we’ll need to modify some of the default layout XML files and add a custom block. Let’s dive into the step-by-step process.
Step 1: Create a Custom Left Menu Layout File
First, navigate to your app/design directory and locate the modern theme folder:
app/design/frontend/ModernTheme/magento-theme/
Next, create a new XML file named catalog_category_leftmenu.xml inside the layout subdirectory. The structure should look like this:
app/design/frontend/ModernTheme/magento-theme/layout/catalog_category_leftmenu.xml
Add the following content to this new XML file:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="category.menu" before="-">
<block class="Magento\Catalog\Block\Navigation" name="catalog.leftmenu"
template="Magento_Catalog::category/leftmenu.phtml"/>
</referenceContainer>
</body>
</page>
Step 2: Modify the Category Page Layout
Now, we need to include our custom layout file in the category page. Open the catalog_category_index.xml file located at:
app/design/frontend/ModernTheme/magento-theme/layout/catalog_category_index.xml
Add the following import statement within the <page> tag:
<import xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"
from="catalog_category_leftmenu"/>
Step 3: Create the Left Menu Template
Next, create a new PHTML file named leftmenu.phtml inside the category subdirectory of your theme’s template folder:
app/design/frontend/ModernTheme/magento-theme/templates/category/leftmenu.phtml
Add the following content to this new PHTML file:
<?php
/** @var Magento\Catalog\Model\Category $category */
$currentCategory = $block->getCurrentCategory();
?>
<div class="category-left-menu">
<h2><?= $block->escapeHtml(__('Categories')) ?></h2>
<?php foreach ($block->getChildBlock('catalog.leftmenu')->getNodes() as $_category): ?>
<a href="<?= $block->escapeUrl($_category->getUrl()) ?>" title="<?= $block->escapeHtml($_category->getName()) ?>">
<?= $block->escapeHtml($_category->getName()) ?>
</a>
<?php endforeach; ?>
</div>
Step 4: Clear Cache and Verify the Changes
Finally, clear your Magento cache by running the following command in your terminal:
bin/magento cache:clean
Now, navigate to any category page on your site. You should see a left-side menu displaying all the sub-categories under that specific category.
Conclusion
In this post, we’ve explored how to create a left-side category menu using the modern theme in Magento. By following these steps, you can enhance your site’s navigation and provide a better user experience for your customers.
Remember, customizing themes requires careful attention to version compatibility and proper testing. If you encounter any issues or need further assistance, feel free to reach out to our team of experienced Magento developers at Laravel Company.
Happy coding!