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 to your block class.
If you are running a Magento 2 store with a large number of product and a huge customer based, 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 2 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 goups 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 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 while following this tutorial.
Enjoyed the tutorial? Spread it to your friends!
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 an influencer wannabe. He is a big fan of sports and travel, also.
Featured Extensions
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