Categories: GeneralHow To

How to Create Categories Tree Structure in Magento Programmatically

When it comes to website navigation, menu behaves like a roadmap to various informative places of the website. Visitors will only stay on website if the menu and navigation is clear and this will be considered as a good user experience. Recently I have been working on my client’s site where he has multiple category structure for each product. We had to custom code to show it in website menu. The main reason behind this was to provide users the thing he needed directly from the home page menu with lesser clicks. This really works well when it comes to providing better user experience and easy navigation to web users.

If you also want to create categories tree structure in Magento, follow this code and paste at the proper place where you want to show the tree structure and show nested categories.

Here is the code to create categories tree structure in Magento programmatically

//CODE FOR MENU START
            function renderCategoriesTree($category) 
   { 
     $children = Mage::getModel('catalog/category')->load($category->getId());
     if($children->getData('children_count'))
     {
      $cl='menu-item-has-children menu-parent-item'; 
     }
     else
     {
      $cl='';
     }
    
     echo '
  • '; echo ""; echo ''.$children->getName().''; echo ""; if($children->getData('children_count')) { echo "
      "; $childr = Mage::getModel('catalog/category')->getCategories($category->getId()); foreach ($childr as $child) { renderCategoriesTree($child); } echo "
    "; } echo '
  • '; } $categories = Mage::getModel('catalog/category') ->getCollection() ->addAttributeToSelect('*') ->addIsActiveFilter() ->addAttributeToFilter('include_in_menu','1') ->addAttributeToFilter('level','2'); foreach($categories as $item) { renderCategoriesTree($item); } //CODE FOR MENU END

    Adding above custom code in Magento will create a categories tree structure like shown below.

    Let me know through commenting if you have any confusion or query regarding this. Your suggestions & feedback are always welcomed. till than, Happy Coding!

    Click to rate this post!
    [Total: 2 Average: 5]
    Dhiren Vasoya

    Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

    Recent Posts

    Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

    Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

    7 hours ago

    Best Beginners Guide to Shopify Balance Account

    If you are a Shopify admin, using a Shopify Balance Account for your business revenue…

    7 hours ago

    8 Best Social Login Apps for Shopify Store in 2024

    Running an eCommerce business can be incredibly demanding, leaving entrepreneurs little time to focus on…

    7 hours ago

    Generating Thumbnails with Spatie Media Library in Laravel 11: A Step-by-Step Guide

    Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…

    1 day ago

    Enhancing Web Application Security with Laravel’s Built-In Features

    In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…

    2 days ago

    Magento 2 Extensions Digest October 2024 (New Release & Updates)

    October was an exciting month for MageComp! From significant updates across our Magento 2 extension…

    2 days ago