How to Get Customer Groups in Magento 2

To get customer groups in Magento 2, I will bring you the simplest way to do that fluently. The topic will show you the following code and please add it into your block class.

How to get customer groups in Magento 2? I will bring you the simplest way to do that fluently. The topic will show you the following code and please add it into your block class.

/**
 * 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;
}

That is all things you will use to retrieve the customer groups in Magento 2. Thanks for your reading and please comment below if there is any trouble in that.

Enjoyed the tutorial? Spread it to your friends!

magento-2-tutorial
retrieve
customer
groups

Sam Thomas
Sam Thomas

CEO and Founder of Mageplaza. Pursueing a simple and healthy lifestyle. A friend, a husband and a dad of two children, a trainer and a wanna-be influencer. He is a big fan of sports and travel, also.

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