In this blog, we are going to talk about how to get a current customer group id in Magento 2.
The current customer group ID has several developer requirements. If a customer is logged in then how to know which group he belongs to we have created this blog.
see this code:
public function __construct(
\Magento\Customer\Model\Session $customer,
\Magento\Framework\App\Http\Context $_httpContext
)
{
$this->customer = $customer;
$this->httpContext = $_httpContext;
}
public function getCurrentCustomerGroupId() {
$isLoggedIn = $this-> ->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
if ($isLogin) {
$currentGroupId = $this->customer->getCustomer()->getGroupId();
} else {
$currentGroupId = 0;
}
return $currentGroupId;
}
⇒ Call phtml file:
$_customerGroupsCollection = $block-> getCurrentCustomerGroupId(); $_customerGroupsCollection->getData();
⇒ Using Object Manager :
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_customerSession = $objectManager->create('Magento\Customer\Model\Session');
if ($_customerSession ->isLoggedIn()) {
//Get current group ID:-
$_customerGroupId = $_customerSession->getCustomer()->getGroupId();
$_groupCollection = $objectManager->create('\Magento\Customer\Model\Group')->load($_customerGroupId);
$_groupCollection->getData();
}
I hope you have understood this blog. If you have any problems then tell me below comment section
Thank You!!..







Leave a Reply