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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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.
1 2 3 4 5 6 | // get customer group collection $customerGroupsCollection = $block->getCustomerGroupCollection(); echo "<pre>"; print_r($customerGroupsCollection); echo "</pre>"; |
2. Using Object Manager
1 2 3 4 5 6 7 8 | $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!!
Leave a Reply