I need to get customer group for each customer in my site. I can get customer group list as below,

$customerGroupsCollection = $objectManager>get('\Magento\Customer\Model\ResourceModel\Group\Collection');
$customerGroups = $customerGroupsCollection->toOptionArray();
foreach ($customerGroups as $key => $value) {
echo $value['label'];

How can I filter this by customer Id? I cannot use CustomerSession to get customer information since I need to retrieve each and every customer's data whenever needed.

I got customer data as follows,

$customer = $objectManager->create('Magento\Customer\Model\Customer')->getCollection();
foreach ($customer as $cusitem) {
$customerId = $cusitem->getData('entity_id');

How can I combine these two and get customer group for each customer?


Have 10 customers(1-10), and have 3 customer groups (A,B,C). How to get each customer's group name?

if customer is 1, How to get 1's group?

  • 1
    try to get customer ID and then try to get customer group ID. – Lasantha Apr 10 '18 at 9:19
  • 1
    Yes as @Lasantha said get customer id and then get group id check my answer – Prince Patel Apr 10 '18 at 10:16
  • @gazorysu let me know if you have any issue – Prince Patel Apr 12 '18 at 5:26

By Object Manager:

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();  
$customers = $objectManager->create('\Magento\Customer\Model\Customer')->getCollection();
$groupRepository  = $objectManager->create('\Magento\Customer\Api\GroupRepositoryInterface');

foreach ($customers as $customer) {
    $customerId = $customer->getEntityId(); // Get customer Id
    $groupId = $customer->getGroupId(); // Get customer group Id
    $group = $groupRepository->getById($groupId); // Get customer group data

    echo $groupCode = $group->getCode(); //Get Customer Group

By Factory Method:

protected $groupRepository;

protected $_customerFactory;

public function __construct(
    \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerFactory
    \Magento\Customer\Api\GroupRepositoryInterface $groupRepository,
) {
    $this->_customerFactory = $customerFactory;
    $this->groupRepository = $groupRepository;

public function getCustomerCollection()
    return $this->_customerFactory->create();

public function getGroupNameById($groupId){
    $group = $this->groupRepository->getById($groupId);
    return $group->getCode();

Now use this function in phtml file

$customers = $block->getCustomerCollection();

foreach ($customers as $customer) {
    echo $customerId = $customer->getEntityId(); // Get customer Id
    $groupId = $customer->getGroupId(); // Get customer group Id
    echo $block->getGroupNameById($groupId);
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.