4

I am working on a custom magento extension.

I've made a selection menu with all customer groups, here is the code of it:

$CustomerGroups = Mage::getResourceModel('customer/group_collection')->toOptionArray();

$CustomerGroups = array_merge(array('' => ''), $CustomerGroups);

$fieldset->addField('customergroups', 'select',
        array(
            'name'      => 'customergroups',
            'label'     => Mage::helper('smsnotification')->__('User Group'),
            'class'     => 'required-entry',
            'values'    => $CustomerGroups
        )
    );

Here is the result:

enter image description here

As you can see the results are like this:

  • not logged in
  • General
  • Wholesale
  • Retailer

What i want is simply to add the number of members in every customer group, for example General - (125 members).

Let me give you an example of what i want to achieve but don't know how in image:

enter image description here

1 Answer 1

4

You need to call customer collection and filter by each Customer group and then create Custom array which is give data for option array

$CustomerGroups = Mage::getResourceModel('customer/group_collection')->toOptionArray();

$customerArray=array();
foreach($CustomerGroups as $each){
    // get Customer group wise Customer count
     $count=Mage::getResourceModel('customer/customer_collection')
                ->addAttributeToFilter('group_id',$each['value'])->getSize();
     $customerArray[]=array('value'=> $each['value'],'label'=> $each['label'].'(member'.$count.')');

}

$CustomerGroups = array_merge(array('' => ''), $customerArray);

Edit

as per as your request your updated code is below:

$CustomerGroups = Mage::getResourceModel('customer/group_collection')->toOptionArray();

$customerArray=array();
foreach($CustomerGroups as $each){

     $count=Mage::getResourceModel('customer/customer_collection')
                ->addAttributeToFilter('group_id',$each['value'])->getSize();
     $customerArray[]=array('value'=> $each['value'],'label'=> $each['label'].'('.$count.' memeber)');

}

$CustomerGroups = array_merge(array('' => ''), $customerArray);

$fieldset->addField('customergroups', 'select',
        array(
            'name'      => 'customergroups',
            'label'     => Mage::helper('smsnotification')->__('User Group'),
            'class'     => 'required-entry',
            'values'    => $CustomerGroups
        )
    );
1
  • Please edit your answer, are you sure all of the code is in the code section of your answer ? Thanks! Commented Jun 1, 2015 at 18:01

Your Answer

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

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