Magento – Magento 2 get specific customer group customers list

customercustomer-groupmagento2magento2.1.5

I have created a custom module and want to get specific customer group users to list in the dropdown.

For this I have gone through the admin > Stores > Other Settings > Customer Groups and I have created a customer group named ex. XYZ Customer. Assigned some users to XYZ Customer group.

Now, I want to list all the user who belongs to the XYZ customer group. How I can list all the users in the custom dropdown.

See below is my custom block code:

<?php
namespace Vendor\Mymodule\Block\Adminhtml\Order\View;

class Custom extends \Magento\Backend\Block\Template
{
    protected $customerCollectionFactory;

    public function __construct(
     \Magento\Backend\Block\Template\Context $context,
     \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerCollectionFactory,
      array $data = []
    ){  $this->customerCollectionFactory  = $customerCollectionFactory;

        parent::__construct($context, $data);
    }

    public function getCustomerCollection()
    {   
        $collection = $this->customerCollectionFactory->create();
        $collection->addFieldToFilter("group_id","4");
        $options = [];
        //echo '<pre>'; print_r($collection); die;
        foreach ($collection as $driver) {
        $options[] = ['label' => $driver->getFirstname()." ".$driver->getLastname(), 'value' => $driver->getEntityId()];
        }
        //echo '<pre>'; print_r($options); die;
        return $options;

    }   
}

Guys help me out this issue. Thanks

Best Answer

Create Block file in module Customer.php

Path : App\Code\{CompanyName}\{ModuleName}\Block\Bookseller\

<?php

namespace {CompanyName}\{ModuleName}\Block\Bookseller;

class Customer extends \Magento\Framework\View\Element\Template
{

    protected $_customerGroupCollection;
    protected $_customerFactory;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context, 
        \Magento\Customer\Model\Group $customerGroupCollection,
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        array $data = []
    ) {
        $this->_customerGroupCollection = $customerGroupCollection;
         $this->_customerFactory=$customerFactory;
        parent::__construct($context, $data);
    }

    public function getCustomerGroup($currentGroupId)
    {
           //Get customer group Id , you have already this so directly get name
            $collection = $this->_customerGroupCollection- 
              >load($currentGroupId); 
            echo $collection->getCustomerGroupCode();//Get group name
            print_r($collection->getData());
    }

    public function getCustomer($customergroupId)
    {
        return $this->_customerFactory->create()->getCollection()- 
             >addFieldToFilter('group_id', $customergroupId);
    }
}

Create phtml file in module Customer.phtml

Path : App\Code\{CompanyName}\{ModuleName}\Block\Bookseller\

<?php
    $groupId = 1;
    $booksellertCollection = $block->getCustomer($groupId);
    echo "<pre>";
    var_dump($booksellertCollection->getData());