Magento2 Customer Group – How to Get Customer Group Name by Using Group ID

customercustomer-groupmagento2template

How can I get customer group name by using group id in a template (phtml file) ?

Best Answer

If you have group id then add below code at your block class

protected $groupRepository;
public function __construct(
    ...
    \Magento\Customer\Api\GroupRepositoryInterface $groupRepository,
    ...
) {
    $this->groupRepository = $groupRepository;
}

public function getGroupName($groupId){
    $group = $this->groupRepository->getById($groupId);
    echo $group->getCode();
}

At last in phtml file you will get group name by:

$block->getGroupName($groupId)

If you want to get group name by group id at phtml using Object manager and donot want edit block class then you can use below code at phtml

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$groupRepository  = $objectManager->create('\Magento\Customer\Api\GroupRepositoryInterface');
$group = $groupRepository->getById([GroupId]);
var_dump($group->getData());
echo $group->getCode();

But direct use of Object manager is not good idea,So best idea to add code at block class.