nested categories dropdown in magento

nested categories dropdown in magento

August 27, 2026 · Illustrated author avatarBy Stefan Bogdanescu
nested categories dropdown in magento

Creating Nested Category Dropdowns in Magento

In this post, we’ll guide you through creating nested category dropdowns for your Magento frontend using a combination of PHP and JavaScript. This will enable customers to select subcategories directly from the dropdown menu.

Current Setup

You’ve already created a working helper method getCategoriesDropdown() that retrieves an array of active categories with their names, levels, and IDs:

public function getCategoriesDropdown() {
    // ... (your existing code)
}

Your PHTML file uses this helper to generate the initial category dropdown:

<select id="category-changer" name="category-changer" style="width:150px;">
  <option value="">--Select Categories--</option>
  <?php
    $_CategoryHelper = Mage::helper("marketplace")->getCategoriesDropdown();
    foreach($_CategoryHelper as $value){
      // ... (remaining code)
    }
  ?>
</select>

This produces a dropdown that looks like this:

Current Category Dropdown

Chaining the Dropdowns

To create nested, chained category dropdowns, you’ll need to make several changes:

  1. Modify your helper method to include parent categories and their children.

  2. Update the PHTML file to handle multiple levels of dropdowns.

  3. Add JavaScript/jQuery to dynamically load subcategory options based on the selected parent.

  4. Ensure the PHP and JavaScript work together seamlessly for a smooth user experience.

Step-by-Step Solution

Let’s go through each step in detail:

1. Enhance the Helper Method

Update your getCategoriesDropdown() method to retrieve all categories with their children:

public function getNestedCategories() {
    $categories = Mage::getModel('catalog/category')
        ->getCollection()
        ->addAttributeToSelect('name')
        ->addAttributeToSort('path', 'asc')
        ->addFieldToFilter('is_active', array('eq' => '1'))
        ->load();

    // Add children to each category
    foreach ($categories as $category) {
        $children = Mage::getModel('catalog/category')->getCategories($category->getId());
        $category->setChildren($children);
    }

    return $categories;
}

2. Update the PHTML File

Use a recursive function in your PHTML file to generate nested dropdown options:

<select id="category-changer" name="category-changer">
  <option value="">--Select Categories--</option>

  <?php
    $_CategoryHelper = Mage::helper("marketplace")->getNestedCategories();
    $this->generateDropdownOptions($_CategoryHelper);
  ?>

  <script type="text/javascript">
    $(document).ready(function() {
      // ... (JavaScript code to handle dropdown changes)
    });
  </script>
</select>

<?php
function generateDropdownOptions($categories, $indent = '') {
  foreach ($categories as $category) {
    echo "<option value='{$category->getId()}'>{$indent}{$category->getName()}</option>";

    if (count($category->getChildren())) {
      generateDropdownOptions($category->getChildren(), $indent . '--');
    }
  }
}
?>

3. Add JavaScript/jQuery

Use jQuery to handle the dynamic loading of subcategories:

$(document).ready(function() {
  $('#category-changer').change(function() {
    var selectedCategoryId = $(this).val();
    if (selectedCategoryId !== '') {
      $.ajax({
        url: '<?php echo $this->getUrl('marketplace/index/getSubCategories') ?>',
        data: { category_id: selectedCategoryId },
        success: function(response) {
          // Update the dropdown with new options
          $('#subcategory-changer').html(response);
        }
      });
    } else {
      $('#subcategory-changer').html('<option value="">--Select Subcategories--</option>');
    }
  });

  // Initial load (if needed)
});

4. Create an Action for AJAX Request

In your Marketplace controller, add an action to handle the AJAX request and return the subcategory options:

public function getSubCategoriesAction() {
    $categoryId = $this->getRequest()->getParam('category_id');
    $subcategories = Mage::getModel('catalog/category')->load($categoryId)->getChildrenCategories();

    // Generate HTML for subcategory dropdown options
    $optionsHtml = '';
    foreach ($subcategories as $subcategory) {
        $optionsHtml .= "<option value='{$subcategory->getId()}'>{$subcategory->getName()}</option>";
    }

    echo $optionsHtml;
}

Conclusion

By following these steps, you’ll have created nested category dropdowns for your Magento frontend. This provides a more intuitive and user-friendly experience for customers browsing products by category.

Remember to adapt the code snippets provided to fit your specific theme and extension setup. If you’d like further guidance or need assistance with any part of this process, feel free to reach out!

For more detailed examples and best practices in Magento development, be sure to check out our blog at Laravel Company. We regularly publish articles on various aspects of e-commerce development for Laravel and Magento.

Catalog Database Themes Customers

Our network