1

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) {
//print_r($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?

Ex:

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?

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

1 Answer 1

1

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);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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