2

Magento 2 Version - Magento 2.1.4 CE

I am trying to create Custom Module for Compulsory login.

When user hits the website url it must needs to redirect on login screen and after logged in user can surf or visit our website.

So Compulsory login functionality i have already done by creating custom module.

Here the issue is how to hide menu ? before login(visitors) ?

enter image description here

i want to hide header menu for visitors. once user is logged in then and then menu will show otherwise it needs to be hide.

Hope it clears.

Update :

I know by overriding the topmenu.phtml i can achieve this. but issue here is if i override topmenu.phtml then myaccount,logout links in dropdown are disappear.

Check below screenshot :

enter image description here

3
  • 1
    @TejabhagavanKollepara care to explain? the linked question does not look like a duplicate at all to me Feb 21, 2017 at 7:08
  • 1
    @Manthan there's a module on Github that does this, maybe it helps: github.com/bitExpert/magento2-force-login Feb 21, 2017 at 7:10
  • @FabianSchmengler Yes You are right !!! for force to login i have used the same module . But for menu i need to do customization and that i have done. Thank you so much Feb 21, 2017 at 8:19

2 Answers 2

2

Override Magento_Theme::view/frontend/templates/html/topmenu.phtml and use this code in the file:

<?php $columnsLimit = $block->getColumnsLimit() ?: 0; ?>
<?php $_menu = $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"}}}'>
        <?php
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $customerSession = $objectManager->get('Magento\Customer\Model\Session');
            if($customerSession->isLoggedIn()) {
               // customer login action
                echo $_menu;
            }
        ?>
        <?php /* @escapeNotVerified */ echo $block->getChildHtml(); ?>
    </ul>
</nav>

Though, using objectmanager is not a good practice, so you may override block file too.

to override topmenu.phtml, follow this:

In your module the template needs to be placed here:

app/code/VENDORNAME/MODULENAME/view/frontend/templates/html/topmenu.phtml

Additionally, a layout definition is required:

app/code/VENDORNAME/MODULENAME/view/frontend/layout/default.xml

and put this in default.xml:

<?xml version="1.0"?>
<page layout="1columns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
  <body>
    <referenceBlock class="Magento\Theme\Block\Html\Topmenu" name="catalog.topnav" template="VENDORNAME_MODULENAME::html/topmenu.phtml" ttl="false"/>
  </body>
</page>
6
  • I know by overriding the topmenu.phtml i can achieve this. but issue here is if i override topmenu.phtml then myaccount,logout links in dropdown are disappear. Feb 20, 2017 at 12:32
  • this must not happen f you override properly. can you show your code which you used to override phtml.
    – Manish Joy
    Feb 20, 2017 at 12:34
  • the same code which you post on above answer Feb 20, 2017 at 12:35
  • ok, I've edited my answer and have mentioned the instructions to override phtml, give it a try.
    – Manish Joy
    Feb 20, 2017 at 12:40
  • I know object manager is not preferd way !! i am just trying to create as demo so no worries . Thanks it working well Feb 20, 2017 at 12:43
1

Check the answer @Raphael How to check if customer is logged in or not in magento 2?

Based on that is the user logged or not, you can change the template of the theme that you are using to show/hide the main menu: The main menu is render here: /vendor/magento/module-theme/view/frontend/templates/html/topmenu.phtml

You can check here how template to be customized: http://devdocs.magento.com/guides/v2.1/frontend-dev-guide/templates/template-overview.html

4
  • how this answer is different from mine?
    – Manish Joy
    Feb 20, 2017 at 11:48
  • In your answer you are using object manager directly, which is bad practice: magento.stackexchange.com/questions/117098/…
    – Nikola
    Feb 20, 2017 at 11:52
  • I had already mentioned that in my answer that using objectManager is not a good practice
    – Manish Joy
    Feb 20, 2017 at 11:53
  • @Nikola I know by overriding the topmenu.phtml i can achieve this. but issue here is if i override topmenu.phtml then myaccount,logout links in dropdown are disappear. Feb 20, 2017 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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