How to Get All Customer Groups in Magento 2
Magento 2 CMS is the preferred platform to build E-commerce stores owing to its features that address modern business requirements.
One such feature is customer groups in Magento 2 that help in targeted customer management, customer-wise marketing efforts, and developing features based on types of customers.
If you are a store owner with a wide variety of products and a large customer base with various customer types, managing customer groups is an essential task, and when you want to implement any functionalities based on the Magento 2 customer groups you need to add customer group field in Magento 2 admin UI component form.
Customer groups let you categorize the store’s customer base into different groups. Once you have grouped customers, you can create rules, tax classes, and group-specific discounts that pertain to individual customer groups.
The Magento 2 offers the following customer group types:
- General
- Not Logged in
- Retailer
- Wholesale
Besides these, one can create a customer group programmatically in Magento 2 based on custom business requirements.
While working with the extensions based on customer groups, one often needs to get all customer groups in Magento 2.
For instance, the admin wants to offer different pricing strategies for wholesalers only and cash on delivery option should only be enabled for VIP customer groups.
Or “instant delivery” is to be offered only to higher customer groups. Or, restrict a payment method for not logged in customer group.
In all such scenarios, one needs to get all customer groups in Magento 2 and use the below solution for the same:
Solution to get all customer groups in Magento 2
Create a Helper file at Vendor/Extension/Helper.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?php namespace Vendore\Extension\Helper; use Magento\Customer\Model\ResourceModel\Group\Collection as CustomerGroup; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context; class Data extends AbstractHelper { protected $customerGroup; public function __construct( Context $context, CustomerGroup $customerGroup ) { $this->customerGroup = $customerGroup; parent::__construct($context); } public function getCustomerGroups() { $customerGroups = $this->customerGroup->toOptionArray(); return $customerGroups; } } |
Use the getCustomerGroups() of helper file, where you want to get all customer groups.
That’s it!
If you have any doubt regarding this post, do mention in the Comments section below.
I would be glad to be of help.
Don’t forget to share this solution with Magento Community via social media.
Thank You.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How to Remove Filterable Attribute Values with 0 Products from Magento 2 Layered Navigation
How to Show Breadcrumbs on Product Page in Magento 2
Next