Magento 2 – How to Filter Customer Group Collection

collection;customercustomer-groupmagento2

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.

Best Answer

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);
Related Topic