3

I want to show customer having specific value for attribute. Suppose I made customer attribute say refcode, now I want to get list of all customer having refcode 1.

How can I add filter to list of customer?

3 Answers 3

10

If refcode is customer eav then you can filter this by function addAttributeFilter('attributecode','fiterablevalue')

1St: Need to load customer collection

 $collection = Mage::getResourceModel('customer/customer_collection');

2nd: then filter by addAttributeToFilter()

$collection->addAttributeToFilter('refcode',1)

3rd: foreach loop to get get list of all customer having refcode 1.

For add an eav attribute to collection you need to use the function addAttributeToSelect('attribute_code')

add

Mage::getResourceModel('customer/customer_collection')
            ->addNameToSelect()
            ->addAttributeToSelect('email')
            ->addAttributeToSelect('created_at')
            ->addAttributeToSelect('refcode');

For getting all field use ->addAttributeToSelect('*') function which is include all eav attribute to collection

Mage::getResourceModel('customer/customer_collection')
            ->addNameToSelect()
            ->addAttributeToSelect('*');

If you want a collection to array then use below function ->__toArray () or ->toArray()

That means to get array of collection use below as exmpale

$collection->toArray();
4
  • wrote below code: $collection = Mage::getResourceModel('customer/customer_collection'); $collection->addAttributeToFilter('refcode',1); echo '<pre>'; print_r($collection); echo '</pre>'; but could not find any customer data like email or else ??
    – mcoder
    Nov 6, 2014 at 16:21
  • then?.....................
    – Amit Bera
    Nov 6, 2014 at 16:22
  • could not get list of customer with above condition , it get large array as ouput i search but there was no email and customer name in whole array
    – mcoder
    Nov 6, 2014 at 16:26
  • i wrote below code :$collection = Mage::getResourceModel('customer/customer_collection') ->addNameToSelect() ->addAttributeToSelect('email') ->addAttributeToSelect('created_at') ->addAttributeToSelect('refcode') ->addAttributeToFilter('refcode',1); print_r($collection); still not able to get email
    – mcoder
    Nov 6, 2014 at 16:42
3

You can get collection of customer through :

$collection = Mage::getModel('customer/customer')->getCollection();

Filter you collection according to attribute :

$collection->addAttributeToFilter('refcode', array('eq' => 1));
2

You can easily add the attribute to the grid, using:

https://github.com/magento-hackathon/GridControl

Then you can filter the grid by the attribute

1

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