0

I want to show the topmenu only when a customer is logged in and I need to use the FPC. When I try to retrieve the customer id with function to create a if statement to see if the customer is logged in, the topmenu.phtml is cached by Magento's FPC.

How can I check if a customer is logged in and display only the topmenu ?

2
  • Hey, depends if you have a theme, but it should be app/design/frontend/module_name/theme/Magento_Theme/templates/html/header.phtml, then you can use $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $customerSession = $objectManager->create('Magento\Customer\Model\Session'); if ($customerSession->isLoggedIn()) {} to check if is logged in and load the header if yes, not the most recommended way but do the trick if you can't find any better option. Hope it helps
    – Rui Silva
    Commented Dec 22, 2020 at 15:56
  • How you add topmenu ? There are any phtml or block file which are depends on it ? Have you use any third party module ? Please Describe more.
    – Msquare
    Commented Dec 23, 2020 at 4:14

2 Answers 2

0

I would suggest two options to achieve this requirement

  1. Use the above code suggested by @JohnnySK, here I would also like to add one point, you can define template in your block.php file instead of layout XML file. It means when customer logged in it will use login-menu.phtml file where you have added the menu code, when customer not logged in then it will use notloggin-menu.phtml. You can use below code to update the template:

protected function _prepareLayout() { parent::_prepareLayout(); $this->setTemplate('Vendor_module::list/product.phtml'); return $this; }

  1. User customer-data section JS variable where you can hide and show the menu when the customer logged in.

How to check that customer is logged in on frontend in Magento 2.1?

Please let me know if you need any other help.

Thanks,

0

You can add layout update in your custom module to add viewModel for catalog.topnav block in default.xml file and also change template for your own instead of Magento_Theme::html/topmenu.phtml, then in viewModel's class just use Magento\Customer\Model\Context class instead of Magento\Customer\Model\Session (and session's method isLoggedIn (it will not work because of depersonalization process)). Your viewModel can look like this:

namespace Vendor\Module\ViewModel;
class MyViewModel implements Magento\Framework\View\Element\Block\ArgumentInterface {

private $httpContext;

public function __construct(
    \Magento\Framework\App\Http\Context $httpContext
) {
    $this->httpContext = $httpContext;
  }

public function getCustomerIsLoggedIn()
{
    return (bool)$this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
}
}

and in custom topmenu.phtml template just wrap $_menuHtml displaying in if statement with method from custom view model (getCustomerIsLoggedIn)

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.