0

Anybody have idea about how can i filter customer group pragmatically ?

I can able to get customer group collection by using below code.

$customerGroupsCollection = $objectManager->create('\Magento\Customer\Model\ResourceModel\Group\Collection');

print_r($customerGroupsCollection->getData()); exit;

Output

[6] => Array
    (
        [customer_group_id] => 9
        [customer_group_code] => first
        [tax_class_id] => 3
        [custom_shipping_amount] => 1200
        [group_creator] => newnew
        [group_creator_id] => 10
        [group_code] => first
        [total_members] => 
        [group_validity] => 
        [members_name] => 
        [member_profile_image] => 
        [group_shopping_amount] => 
        [group_image] => /k/o/koala_1.jpg
        [group_link] => first
    )

Now i am trying to apply addAttributeToFilter but its not working !?

Anyone know what is missing ?

$customerGroupsCollection = $objectManager->create('\Magento\Customer\Model\ResourceModel\Group\Collection');

$customerGroupsCollection->addAttributeToSelect('*')->addAttributeToFilter('group_link',['eq'=>'thisisfreshgroup']);
print_r($customerGroupsCollection->getData()); exit;

Getting unDefined Method addAttributeToFilter.

1

Try this,

    $customerGroupsCollection = $objectManager->create('\Magento\Customer\Model\ResourceModel\Group\Collection');

    $customerGroupsCollection->addFieldToFilter('group_link',['eq'=>'thisisfreshgroup']);

    $collection = $customerGroupsCollection->getData();

    foreach ($collection as $value) {
       $customer_group_id[] = $value['customer_group_id'];
     }
    print_r($customer_group_id);
  • It's working, I totally forgot about addFieldToFilter(), But i also have to remove addAttributeToSelect to make it work. Thanks. – anonymous Mar 5 '18 at 10:38
  • @LazyCoder please accept the answer, this is working for you and this will helpful for others – Jeeva Chezhiyan Mar 5 '18 at 10:43
  • I'm unable to get customer group Id, I try with echo $customerGroupsCollection->getCustomerGroupId(); – anonymous Mar 5 '18 at 10:52
  • What error coming? – Jeeva Chezhiyan Mar 5 '18 at 10:53
  • Call to undefined method Magento\Customer\Model\ResourceModel\Group\Collection::getCustomerGroupId(); – anonymous Mar 5 '18 at 10:55
1

One should use dependency injection and refrain from using objectManager directly.

First inject customer collection factory to constructor :

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerFactory 
){
    $this->customerFactory = $customerFactory;
    parent::__construct($context);
}

Then add a method to retrieve collection :

public function getFreshCustomers(){
    $customerCollection = $this->customerFactory->create();
    $customerCollection->addFieldToFilter('group_id',6);
    return $customerCollection;
}

Where 6 is the group id one is looking for.

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.