In this post I am going to explain how to get customer group collection.
1. Using Dependency Injection
Add below code snippet in Block class.
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() {
$customerGroupsCollection = $this->_customerGroupCollection->toOptionArray();
return $customerGroupsCollection;
}
Add below code snippet in template file.
// get customer group collection $customerGroupsCollection = $block->getCustomerGroupCollection(); echo "<pre>"; print_r($customerGroupsCollection); echo "</pre>";
2. Using Object Manager
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
// get customer group collection
$customerGroupsCollection = $objectManager->get('\Magento\Customer\Model\ResourceModel\Group\Collection')->toOptionArray();
echo "<pre>";
print_r($customerGroupsCollection);
echo "</pre>";Thats it. Enjoy Magento 2!!
Write an article about ecommerce that help people to grow their ecommerce business. You’ll find best ecommerce guide, news, tips & more!


Leave a Reply