Displaying Magento Categories and Sub-Categories in Sidebar
In Magento, displaying categories and sub-categories in the sidebar involves modifying your theme’s layout files and using Magento’s built-in category rendering functionality. This guide will walk you through the process of creating a two-level category sidebar menu.
Step 1: Enable the Left Column for Your Website
First, ensure that your website has a left column enabled where you can place the sidebar menu:
- Log in to your Magento admin panel.
- Navigate to Stores > Configuration > General > Design.
- Under “Default Configurations”, select your store view.
- Scroll down to the Layout section and click on “Add Layout Update”.
- Enter
your_theme(replaceyour_themewith your actual theme name) in the Select field. - In the XML layout field, add or update the following code:
<referenceContainer name="header.container" as="header_links">
<block class="Magento\Theme\Block\Html\Topmenu" name="catalog.topnav"/>
</referenceContainer>
- Click “Save Layout”.
- Finally, click “Save Config”.
Step 2: Create a Two-Level Category Sidebar
To create a two-level category sidebar menu:
-
Open your theme’s
app/design/frontend/{Your_Vendor}/{Your_Theme}/Magento_Catalog/layout/catalog_category_view.xmlfile.Replace
{Your_Vendor}and{Your_Theme}with your actual vendor name and theme name, respectively. -
Add the following code inside the
<referenceContainer>tags:
<referenceContainer name="aside.additional">
<block class="Magento\Catalog\Block\Category\View" name="catalog.category.tree"
template="Magento_Catalog::category/tree.phtml"/>
</referenceContainer>
- Save and close the file.
The above steps add a new container named aside.additional where your category tree will be rendered. The catalog.category.tree block is responsible for rendering the category tree using the default Magento layout structure.
Step 3: Customizing the Category Tree Template
If you want to customize the look and feel of the sidebar categories:
-
Open or create a new file in your theme directory at
app/design/frontend/{Your_Vendor}/{Your_Theme}/Magento_Catalog/templates/category/tree.phtml. -
Copy the existing contents from
/vendor/magento/module-catalog/view/frontend/templates/category/tree.phtmland paste it into your newly created file. -
You can now modify this template file to match your design requirements while keeping the original Magento logic intact.
Conclusion
To recap, displaying categories and sub-categories in a sidebar menu involves enabling the left column for your website, modifying the layout files to include the category tree block, and optionally customizing the look of the category tree using a theme-specific template.
Remember that proper theming practices are crucial to ensure the stability and performance of your Magento store. Always backup your files before making changes, and thoroughly test any modifications you make in a development environment before deploying them live.
For more advanced customizations or if you need professional assistance with your Magento development, don’t hesitate to contact our expert team at Laravel Company. We specialize in providing high-quality, secure, and optimized Magento solutions tailored to meet your business needs.