2

I want collection of orders which belongs from customer_group_id = 4 or customer_group_id = 5. I have applied following filter on collection.

->addFieldToFilter('customer_group_id',Mage::getSingleton('customer/session')->getCustomer()->getGroupId())

This will filter the current user's customer_group_id with database's customer_group_id.

But what i want is, I want to filter on customers those who has customer_group_id equal to either '4' or '5'.

My question is, How should i apply this addAttributeToFilter to achieve that collection.

2 Answers 2

3

If you want to filter order collection then,you need to

use addFieldToFilter('filterablecolumn','filtertableCondition')

and as you want collection filter by two group_id ids then need use in array in collection.

$collection->addFieldToFilter('field_name', array(
    'in' => array('Val1','Val2','Val2'),

Then try this:

$collection = Mage::getResourceModel('sales/order_collection')
            ->addNameToSelect()
            ->addFieldToFilter('customer_group_id', array(
        'in' => array(5,6);

Or:

 $collection=Mage::getResourceModel('sales/order_collection');
 $collection->getSelect()->where('customer_group_id = ?', 4);
 echo $collection->getSelect()->orWhere('customer_group_id = ?', 5);

See how to use collection filter in magento

EDIT

1
  • First of all thank you very much for the answer amit. But here is one more twist. {->addFieldToFilter('customer_group_id',Mage::getSingleton('customer/session')->getCustomer()->getGroupId()) } here what i am doing is .. checking customer_group_id from session. What i want is. whatever i am gettting from "Mage::getSingleton('customer/session')->getCustomer()->getGroupId()" should be equal to 5 or 6.
    – Viraj
    Jun 28, 2015 at 6:30
2

You can try below piece of code to get the customer collection belongs to customer group 4 or 5.

$collection = Mage::getResourceModel('customer/customer_collection');
$collection->getSelect()->where(new Zend_Db_Expr("(e.group_id = 4 OR e.group_id = 5)"));

or

$collection = Mage::getResourceModel('customer/customer_collection')->addFieldToFilter('group_id', array('in' => array(2, 4)));
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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