0

Hello I want to display a "Default Category" in nav main menu and all subcategory inside this. I know this question has been solved but I do not know how to do that

i have a template in vendor topmenu.html

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/**
 * Top menu for store
 *
 * @var $block \Magento\Theme\Block\Html\Topmenu
 */

$columnsLimit = $block->getColumnsLimit() ?: 0;
$_menuHtml = $block->getHtml('level-top', 'submenu', $columnsLimit)
?>

<nav class="navigation" data-action="navigation">
    <ul data-mage-init='{"menu":{"responsive":true, "expanded":true, "position":{"my":"left top","at":"left bottom"}}}'>
        <?= /* @noEscape */ $_menuHtml?>
        <?= $block->getChildHtml() ?>
    </ul>
</nav>

I see this solution but i don't know to add this because i have a error when i add this code in override topmenu Error: Class 'Mage' not found

  <?php

  $root_category = Mage::getModel('catalog/category')->load(3); // Put your root category ID here.
  $subcategories = $root_category->getChildren();
  foreach(explode(',',$subcategories) as $subcategory) {
        $category = Mage::getModel('catalog/category')->load($subcategory);
        echo '<a href="'.$category->getURL() .'" />'.$category->getName().'</a><br/>';
  }

  ?>

Please how to do to display root category and submenu in main menu

thanks

9
  • Default category is static you could not find it in database. so you should write default category as satic.
    – S.P
    Commented Apr 19, 2022 at 5:15
  • @S.P Thanks how to write default category in static to be able to display it?
    – pleonasme
    Commented Apr 19, 2022 at 6:34
  • add echo '<a href="#" />Default Category </a><br/>'; at the top of your function.
    – S.P
    Commented Apr 19, 2022 at 6:38
  • please can you rewrite a template topmenu.phtml i don't undesrtand and why i have a error Error: Class 'Mage' not found
    – pleonasme
    Commented Apr 19, 2022 at 6:49
  • Might be your are using magento1.x,
    – S.P
    Commented Apr 19, 2022 at 6:54

1 Answer 1

0

Magento2 does not use Mage class,

You should use category factory object (Magento\Catalog\Model\ResourceModel\Category\CollectionFactory) in block constructor

$objectManager = $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$categoryFactory = $objectManager->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
$categories = $categoryFactory->create()                              
    ->addAttributeToSelect('*')
    //->setStore($this->_storeManager->getStore())
      ; 

foreach ($categories as $category){
    $category->getName();
}

Reference How to get Category Collection per Store in Magento 2?

https://www.rakeshjesadiya.com/how-to-get-category-collection-by-category-id-in-magento-2/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.