4

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

0

You can use filter in customer collection as follows

->addFieldToFilter('customer_group_id','1');

where 1 is customer group id.

  • But how I can get customer collection first? Could you please let me know complete process. I have created a block in my custom module to display the dropdown list. – mageDev0688 May 30 '17 at 10:23
  • Please paste your code in question by editing it – Kishan Patadia May 30 '17 at 10:23
  • You can see the complete module code in this Question. – mageDev0688 May 30 '17 at 10:26
  • After this I want to save these customer ID in custom table as well in respect of order id. – mageDev0688 May 30 '17 at 10:29
  • please check the updated question. I have tried with your code ->addFieldToFilter('customer_group_id','1');, but when I use the customer_group_id the function will stuck. – mageDev0688 May 30 '17 at 12:26
0
protected $_customerCollectionFactory;

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerCollectionFactory,
    array $data = []
)
{    
    $this->_context                    = $context;
    $this->_customerCollectionFactory  = $customerCollectionFactory;  
    parent::__construct($context, $data);
}

public function getCustomerCollection() {

    $collection = $this->_customerCollectionFactory->create();
    $collection->addFieldToFilter("group_id","3");
    return $collection->getData();
}
0

You can get customer group details and specific group member's customer ids by doing like this

 class Trial extends \Magento\Framework\View\Element\Template
{
protected $_customerGroup;
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);
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.