cancel
Showing results for 
Search instead for 
Did you mean: 

Magento2.1 make parent category link not clickable

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

Magento2.1 make parent category link not clickable

I have seen examples of how to do this in 1.x . However I am wondering how I would make a category link that has subcategories under it not clickable. So basically the parent category is not a clickable link? I am not sure of the location of the file to edit also how the code would be modified for 2.1 ? 

 

I know in Magento 1.x you would do something like the below. However I am still trying to master Magento2.1

 

// ADD THESE LINES
if (!empty($_hasChildren)) {
     $html .= '<a onclick="return false" style="text-decoration:none; cursor: default;" href="javascript&colon;;" class="'. $outermostClassCode .' '. $_hasChildren .'">'. $this->escapeHtml($this->__($child->getName())) .'</a>';
     } else {
       $html .= '<a href="'. $child->getUrl() .'" class="'. $outermostClassCode .' '. $_hasChildren .'">'. $this->escapeHtml($this->__($child->getName())) .'</a>';
     }

 

 

2 REPLIES 2

Re: Magento2.1 make parent category link not clickable

You can remove Top Navigation with main link with below changes,


You just need to override Topmenu.php file from

vendor/magento/module-theme/Block/Html/Topmenu.php

to your custom module.

Replace _getHtml() function with below code,

protected function _getHtml(
        \Magento\Framework\Data\Tree\Node $menuTree,
        $childrenWrapClass,
        $limit,
        $colBrakes = []
    ) {
        $html = '';

        $children = $menuTree->getChildren();
        $parentLevel = $menuTree->getLevel();
        $childLevel = $parentLevel === null ? 0 : $parentLevel + 1;

        $counter = 1;
        $itemPosition = 1;
        $childrenCount = $children->count();

        $parentPositionClass = $menuTree->getPositionClass();
        $itemPositionClassPrefix = $parentPositionClass ? $parentPositionClass . '-' : 'nav-';

        foreach ($children as $child) {
            $child->setLevel($childLevel);
            $child->setIsFirst($counter == 1);
            $child->setIsLast($counter == $childrenCount);
            $child->setPositionClass($itemPositionClassPrefix . $counter);

            $outermostClassCode = '';
            $outermostClass = $menuTree->getOutermostClass();

            if ($childLevel == 0 && $outermostClass) {
                $outermostClassCode = ' class="' . $outermostClass . '" ';
                $child->setClass($outermostClass);
            }

            if (count($colBrakes) && $colBrakes[$counter]['colbrake']) {
                $html .= '</ul></li><li class="column"><ul>';
            }

            $html .= '<li ' . $this->_getRenderedMenuItemAttributes($child) . '>';
            if($child->getLevel() > 0){
            $html .= '<a data-test="'.$child->getLevel().'" href="' . $child->getUrl() . '" ' . $outermostClassCode . '><span>'
                . $this->escapeHtml($child->getName())
                . '</span></a>' . $this->_addSubMenu(
                    $child,
                    $childLevel,
                    $childrenWrapClass,
                    $limit
                ) . '</li>';
            }else{
            $html .= '<a href="javascript&colon;void(0)" ' . $outermostClassCode . '><span>'
                . $this->escapeHtml($child->getName())
                . '</span></a>' . $this->_addSubMenu(
                    $child,
                    $childLevel,
                    $childrenWrapClass,
                    $limit
                ) . '</li>';
            }
            $itemPosition++;
            $counter++;
        }

        if (count($colBrakes) && $limit) {
            $html = '<li class="column"><ul>' . $html . '</ul></li>';
        }

        return $html;
    }

if issue solved, Click Kudos/Accept as solutions.

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: Magento2.1 make parent category link not clickable

When I replace the gethtml() public function in the vendor/magento/module-theme/Block/Html/Topmenu.php folder with the one you posted above I get a 500 error?