Magento – how to get customer group name using customer id magento2

magento2

I am not able to get Customer group name using customer id in magento2.
i have customer group id but i didn't get group name.

<?php 
    $customerObj = $objectManager->create('Magento\Customer\Model\Customer')->getCollection()->addFieldToFilter('entity_id',  $group_id);

    print_r($customer_data->getCustomerData());
?>

Best Answer

Try below code :

protected $_customerSession;

protected $_customerGroupCollection;

public function __construct(
    ....    
    \Magento\Customer\Model\Session $customerSession,
    \Magento\Customer\Model\Group $customerGroupCollection,
    ....
) {


    $this->_customerSession = $customerSession;
    $this->_customerGroupCollection = $customerGroupCollection;

}

public function getCustomerGroup()
{
        echo $currentGroupId = $this->_customerSession->getCustomer()->getGroupId(); //Get customer group Id , you have already this so directly get name
        $collection = $this->_customerGroupCollection->load($currentGroupId); 
        echo $collection->getCustomerGroupCode();//Get group name
}