0

As a new Magento developer. Every day I face new problems the problem I faced today is creating my own mega menu and I find it very difficult. As I can use the extension but I don't like the idea of using the extension. So I decided to customise and create a mega menu. so I researched and read this thread but I couldn't find my answer besides a provided link is not working anymore. so I wanted to make this Present Magento Menu to this in my custom theme My wanted Magento Menu. So now in topmenu.php of Magento\Theme\Block\Html I am afraid to change anything if I broke something. Present Magento Menu Code enter image description here how can I replace This left-side code style with This right-side code style in Topmenu.php I am not an expert in Magento so if I change something without knowing it could break anything. So if anyone expert could help me out to accomplish my goal. Thanks in advance.

asked Oct 21, 2021 at 8:04

1 Answer 1

0

With the help of CategoryFactory you can get a collection of your categories, like so:

 public function getChildCategories($categoryId)
 {
 $category = $this->categoryFactory->create();
 $categoryCollection = $category->load($categoryId);
 //Get category collection
 return $categoryCollection->getCollection()
 ->addNavigationMaxDepthFilter()
 ->addUrlRewriteToResult()
 ->addIsActiveFilter()
 ->addAttributeToFilter('include_in_menu', 1)
 ->addAttributeToSelect(['name', 'url'])
 ->setOrder('position', 'ASC')
 ->addIdFilter($categoryCollection->getChildren());
 }

In your template you can then loop through the collection and build your mega menu.

answered Oct 22, 2021 at 16:15
1
  • Thanks for your reply. Do I need to add this code alongside the existent Topmenu.php or create a new Topmenu.php? Commented Oct 27, 2021 at 7:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.