Shopping cart

How to Get Customer Groups in Magento 2

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

Brian Tran
Brian Tran
Senior Magento Developer who is passionate about development and love technology

Comments for How to Get Customer Groups in Magento 2

Please leave comments if you have any questions, feedbacks.

Module Development

We recommend:

You also may like these Magento 2 Extensions

One Step Checkout

$199
120 reviews

Layered Navigation

$99
61 reviews

Gift Card

$199
8 reviews

Reward Points

$99
8 reviews

Affiliate

$149
16 reviews

Shop By Brand

$99
19 reviews

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
  • magento 2 get customer group name
  • 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