{"id":7183,"date":"2019-09-18T16:35:57","date_gmt":"2019-09-18T11:05:57","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=7183"},"modified":"2019-09-27T09:49:18","modified_gmt":"2019-09-27T04:19:18","slug":"magento-2-get-customer-group-collection","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-get-customer-group-collection\/","title":{"rendered":"Magento 2 &#8211; How to Get Customer Group Collection"},"content":{"rendered":"<p>In this post I am going to explain how to get customer group collection.<\/p>\n<p><strong>1. Using Dependency Injection<\/strong><\/p>\n<p>Add below code snippet in Block class.<\/p>\n<pre class=\"lang:default decode:true \">protected $_customerGroupCollection;\r\n    \r\npublic function __construct(\r\n\t\\Magento\\Backend\\Block\\Template\\Context $context,\r\n\t\\Magento\\Customer\\Model\\ResourceModel\\Group\\Collection $customerGroupCollection,        \r\n\tarray $data = []\r\n)\r\n{\r\n\t$this-&gt;_customerGroupCollection = $customerGroupCollection;        \r\n\tparent::__construct($context, $data);\r\n}\r\n\r\npublic function getCustomerGroupCollection() {\r\n\t$customerGroupsCollection = $this-&gt;_customerGroupCollection-&gt;toOptionArray();\r\n\treturn $customerGroupsCollection;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Add below code snippet in template file.<\/p>\n<pre class=\"lang:default decode:true \">\/\/ get customer group collection\r\n$customerGroupsCollection = $block-&gt;getCustomerGroupCollection();\r\n\r\necho \"&lt;pre&gt;\";\r\nprint_r($customerGroupsCollection);\r\necho \"&lt;\/pre&gt;\";<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>2. Using Object Manager<\/strong><\/p>\n<pre class=\"lang:default decode:true \">$objectManager = \\Magento\\Framework\\App\\ObjectManager::getInstance();\r\n\r\n\/\/ get customer group collection\r\n$customerGroupsCollection = $objectManager-&gt;get('\\Magento\\Customer\\Model\\ResourceModel\\Group\\Collection')-&gt;toOptionArray();\r\n\r\necho \"&lt;pre&gt;\";\r\nprint_r($customerGroupsCollection);\r\necho \"&lt;\/pre&gt;\";<\/pre>\n<p>Thats it. Enjoy Magento 2!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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-&gt;_customerGroupCollection = $customerGroupCollection; parent::__construct($context, $data); } public function getCustomerGroupCollection() { $customerGroupsCollection = $this-&gt;_customerGroupCollection-&gt;toOptionArray(); return $customerGroupsCollection;&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-get-customer-group-collection\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":7312,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[656,657,10,28,27],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7183"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=7183"}],"version-history":[{"count":1,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7183\/revisions"}],"predecessor-version":[{"id":7184,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7183\/revisions\/7184"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/7312"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=7183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=7183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=7183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}