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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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:
1 2 |
$_customerGroupsCollection = $block-> getCurrentCustomerGroupId(); $_customerGroupsCollection->getData(); |
⇒ Using Object Manager :
1 2 3 4 5 6 7 8 9 10 11 12 |
$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