In this blog, we are going to talk about how to get a customer group in Magento 2.
Why? Customer groups are defined in Magento 2. Because many developers set the configuration on the customer. Like
Different customers have different configurations of payment methods, shipping methods, specific offers, restricted products, restricted categories, etc.
So customer get is required. and so we are creating this blog. Use this code in helper or blog.
Put this code in your file:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
protected $_customerGroupCollection; public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Customer\Model\ResourceModel\Group\Collection $_customerGroupCollection, array $data = [] ) { $this->_customerGroupCollection = $_customerGroupCollection; parent::__construct($context, $data); } public function getCustomerGroupCollection() { $_customerGroupCollection = $this->_customerGroupCollection->toOptionArray(); return $_customerGroupCollection; } |
⇒ Call Phtml file :
1 2 3 4 5 |
// get customer group collection $_customerGroupsCollection = $block->getCustomerGroupCollection(); $_customerGroupsCollection->getData(); |
⇒ Using Object manager :
1 2 3 4 5 |
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // get customer group collection $_customerGroupsCollection = $objectManager->get(‘\Magento\Customer\Model\ResourceModel\Group\Collection’)->toOptionArray(); $_customerGroupsCollection->getData(); |
I hope you have understood this blog. If you have any problems then tell me below comment section
Thank You!!..