Magento2 Get Customer Group ID in topmenu.phtml
-
15-04-2021 - |
Frage
I am needing to show a totally different navigation bar links if a customer is logged in as a wholesaler customer group.
I am using Magento2.3 and a child theme of Luma. What I did was edit the topmenu.phtml as seen below to get the customer group id and then if the customer group id is a specific # to show a static block My issue is no matter what I clear cache etc it still just shows the customer id as 0 regardless if I am logged in or not. Yet the same code works in the footer.phtml and other files just not in the topmenu.phtml file?
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->create('Magento\Customer\Model\Session');
if ($customerSession->isLoggedIn())
{
$customerGroupId = $customerSession->getCustomer()->getGroupId();
}
else
{
$customerGroupId = 0;
}
$columnsLimit = $block->getColumnsLimit() ?: 0;
$_menuHtml = $block->getHtml('level-top', 'submenu', $columnsLimit)
?>
<nav class="navigation" data-action="navigation">
<?php
//if user is a wholesaler
if($customerGroupId == 2)
{
echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('wholesale-top-nav-bar')->toHtml();
}
else
{
?>
<ul data-mage-init='{"menu":{"responsive":true, "expanded":true, "position":{"my":"left top","at":"left bottom"}}}'>
<?= /* @noEscape */ $_menuHtml?>
<?= $block->getChildHtml() ?>
</ul>
<?php
}
?>
</nav>
Lösung
Assume that your phtml
name in the layout is catalog.topnav
I suggest that you have to use Magento\Framework\App\Http\Context
class to get customer group id to get customer group id at your template file.
Create default.xml
at your module and add a class a view model and pass customer group from view model to phtml file.
View Model Class:
<?php
namespace {Vendorname}\{Modulename}\ViewModel;
use Magento\Framework\App\Http\Context;
use Magento\Framework\View\Element\Block\ArgumentInterface;
use Magento\Customer\Model\Context as CustomerContext;
class CustomerGroupInfo implements ArgumentInterface
{
/**
* @var Context
*/
private $httpContext;
public function __construct(Context $httpContext)
{
$this->httpContext = $httpContext;
}
public function getCustomerGroupId()
{
return $this->httpContext->getValue(CustomerContext::CONTEXT_GROUP);
}
}
Add this view model to layout for customer group id in your phtml file from default.xml.
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceBlock name="catalog.topnav"> <arguments> <argument name="customer_view_model" xsi:type="object">{Vendorname}\{Modulename}\ViewModel\CustomerGroupInfo</argument> </arguments> </referenceBlock> </body> </page>
Then on phtml
on access by getCustomerGroupId
below code:
<?php
/** @var $viewModel \{Vendorname}\{Modulename}\ViewModel\CustomerGroupInfo */
$viewCustomerModel = $block->getCustomerViewModel();
$viewCustomerModel->getCustomerGroupId();
?>
Andere Tipps
You have to use session factory. Using session is a singleton. If I was to do it in the code you presented
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->create('Magento\Customer\Model\SessionFactory')->create();
if ($customerSession->isLoggedIn())
{
$customerGroupId = $customerSession->getCustomerData()->getGroupId();
}
else
{
$customerGroupId = 0;
}
$columnsLimit = $block->getColumnsLimit() ?: 0;
$_menuHtml = $block->getHtml('level-top', 'submenu', $columnsLimit)
?>
<nav class="navigation" data-action="navigation">
<?php
//if user is a wholesaler
if($customerGroupId == 2)
{
echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('wholesale-top-nav-bar')->toHtml();
}
else
{
?>
<ul data-mage-init='{"menu":{"responsive":true, "expanded":true, "position":{"my":"left top","at":"left bottom"}}}'>
<?= /* @noEscape */ $_menuHtml?>
<?= $block->getChildHtml() ?>
</ul>
<?php
}
?>
</nav>