How to Get Customer Groups in Magento 2

If you are running a Magento 2 store with a large number of products and a huge customer base, it’s essential to categorize customers into groups. Customer groups help you manage customers better. You can understand and serve them better with more relevant and personalized offers.

Magento supports three default customer groups:

  • General
  • Not Logged in
  • Wholesale

However, it’s also easy and quick to create extra customer groups in Magento 2. It usually depends on the demands of each store owner.

In this post, I will bring you the simplest way to get customer groups in Magento 2 smoothly. To do that, please copy the following code and add it to your block class.

Get customer groups in Magento 2

/**
 * Customer Group
 *
 * @var \Magento\Customer\Model\ResourceModel\Group\Collection
 */
protected $_customerGroup;
    
/**
 * @param \Magento\Backend\Block\Template\Context $context
 * @param \Magento\Customer\Model\ResourceModel\Group\Collection $customerGroup
 * @param array $data
 */
public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Customer\Model\ResourceModel\Group\Collection $customerGroup,        
    array $data = []
) {
    $this->_customerGroup = $customerGroup;        
    parent::__construct($context, $data);
}
/**
 * Get customer groups
 * 
 * @return array
 */ 
public function getCustomerGroups() {
    $customerGroups = $this->_customerGroup->toOptionArray();
    array_unshift($customerGroups, array('value'=>'', 'label'=>'Any'));
    return $customerGroups;
}

Wrap up

That is everything you will use to retrieve the customer groups in Magento 2. Thanks for your reading and please comment below if there is any trouble while following this tutorial.

Enjoyed the tutorial? Spread it to your friends!

magento-2-tutorial
retrieve
customer
groups

Sam Thomas
Sam Thomas

Sam is the CEO and Founder of Mageplaza, pursuing a simple and healthy lifestyle. He is a friend, a husband, and a father to two children. As a trainer and an aspiring influencer, he is also a big fan of sports and travel. Sam is exceptionally knowledgeable about e-commerce, especially Magento and Shopify, with 10 years of experience in the field.

People also searched for

  • magento 2 retrieve customer groups
  • magento 2 get customer group name
  • magento 2 create customer programmatically
  • magento 2 get current customer group
  • magento restrict products by customer group
  • how to get customer group name id in magento 2
  • how to create customer groups in magento 2
  • how to add customer group in magento 2
  • how to change customer group in magento 2
  • how to check customer group in magento 2
  • how to manage customer group in magento 2
  • magento 2 get customer group programmatically
  • get customer group id magento 2
  • magento 2 create customer group programmatically
  • magento 2 get customer group
  • magento 2 get customer group id
  • magento 2 get customer group name by id
  • magento 2 get all customer groups
  • magento 2 change customer group programmatically
  • 2.2.x, 2.3.x, 2.4.x