How to Remove Specific Category From Category Menu programmatically in Magento 2?

How to Remove Specific Category From Category Menu programmatically in Magento 2

Magento 2 has a well-defined category structure that makes it functional. But sometimes, you might want to exclude one category from the category menu for a better user experience. Even though there is no direct option available in the Magento admin panel, it can be done programmatically by modifying the menu structure.

This tutorial describes the steps you need to take in order to find the category you want to exclude and modify the menu rendering logic accordingly. This solution is for developers and store owners who need a technical fix with which you can manage category visibility precisely. 

Implementing these modifications will enhance navigation and raise the general shopping experience level for your customers.

Magento version upgrade

Steps to Remove Specific Category from Category Menu

Step 1 :- Before starting the steps, you must create “di.xml” file inside our extension at the following path. 👇

Path:- app\code\Vendor\Extension\etc\di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Catalog\Plugin\Block\Topmenu"
        type="Vendor\Extension\Plugin\Categorymenu"/>
</config>
Magento Malware Removal

Step 2 :- After this, create “Categorymenu.php” file inside our extension at the following path.

Path:- app\code\Vendor\Extension\Plugin\Categorymenu.php

<?php

declare(strict_types=1);

namespace Vendor\Extension\Plugin;

use Magento\Catalog\Model\Category;
use Magento\Framework\Data\Collection;
use Magento\Framework\Data\Tree\Node;
use Magento\Catalog\Model\ResourceModel\Category\StateDependentCollectionFactory;

class Categorymenu extends \Magento\Catalog\Plugin\Block\Topmenu
{

    private $collectionFactory;
    private $storeManager;
    private $layerResolver;

    public function __construct(
        \Magento\Catalog\Helper\Category $catalogCategory,
        StateDependentCollectionFactory $categoryCollectionFactory,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Catalog\Model\Layer\Resolver $layerResolver
    ) {
        $this->catalogCategory = $catalogCategory;
        $this->collectionFactory = $categoryCollectionFactory;
        $this->storeManager = $storeManager;
        $this->layerResolver = $layerResolver;

        parent::__construct(
            $catalogCategory,
            $categoryCollectionFactory,
            $storeManager,
            $layerResolver
        );
    }

    protected function getCategoryTree($storeId, $rootId)
    {

        $categoryCollection = $this->collectionFactory->create()
        ->setStoreId($storeId)
        ->addAttributeToSelect('name')
        ->addAttributeToFilter('name', ['neq' => 'Gear']) /*Replace with your category name*/ */
        ->addFieldToFilter('path', ['like' => "1/{$rootId}/%"])
        ->addAttributeToFilter('include_in_menu', 1)
        ->addIsActiveFilter()
        ->addNavigationMaxDepthFilter()
        ->addUrlRewriteToResult()
        ->setOrder('level', Collection::SORT_ORDER_ASC)
        ->setOrder('position', Collection::SORT_ORDER_ASC)
        ->setOrder('parent_id', Collection::SORT_ORDER_ASC)
        ->setOrder('entity_id', Collection::SORT_ORDER_ASC);

        return $categoryCollection;
    }
}

Conclusion

By programmatically disabling a particular category from the category menu in Magento 2, you have more control over your store’s navigation without impacting backend category administration. This method enables you to cleanse the user interface, displaying only pertinent categories in the menu. 

Proper implementation of this method improves the usability and look of your store and leaves room for future adjustments. If you are handling an intricate product catalog, such customizations can prove to be priceless in site navigation optimization and directing the customers to the proper products as efficiently as possible.

Hire Magento Programmer

Hope you found this tutorial informative. If you have any questions or need further assistance, feel free to contact us or leave a comment below!

Previous Article

How to Implement an Image Slider in React Native?

Next Article

Gemini Statistics: Latest Facts and Figures (2025 Edition)

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨