How to get current customer group id in magento2

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

Leave a comment

Your email address will not be published. Required fields are marked *