Magento 2 – How to Get Current Customer Group ID

customer-groupfrontendmagento2

I want to get current customer group id in phtml file. When I am not logged in still it is return general type customer group. How can get proper output?

Best Answer

Magento\Customer\Model\Session $customerSession using this class you will get the current customer group id

protected $_customerSession;

public function __construct(
        \Magento\Customer\Model\Session $customerSession,
    ) {
        $this->_customerSession = $customerSession;
    }

public function getGroupId(){
 if($this->_customerSession->isLoggedIn()):
        echo $customerGroup=$this->_customerSession->getCustomer()->getGroupId();
    endif;
}

NOTE: You only get customer id if the customer logged in

Related Topic