1

I have around 80 different customer groups, with different prices on.

How can I find out how many customers are in any given group?

Can I make a list of all customers included in customer group?

I have this but it gives me error Fatal error: Class 'Mage' not found in /domains/dev.vampirevape.co.uk/http/customer_groups.php on line 3

UPDATE: adding:

require_once('app/Mage.php');
Mage::app();

Fixed it.

Update 2:

Error: Fatal error: Call to a member function addField() on a non-object in /domains/dev.vampirevape.co.uk/http/customer_groups.php on line 19

Where do I have to place the code to get the result. I found this code and this supposed to be a outcome but I'm struggling implementing it.

UPDATE 3

This is my code, it displays almost everything exactly as I want, except, there are gaps in between the groups, also could I have a group ID with it too?

enter image description here

require_once('app/Mage.php');
Mage::app();


$_customer = array();
$customerCollection = Mage::getResourceModel('customer/customer_collection');
foreach ($customerCollection as $customer) {
    $_customer[$customer->getGroupId()][] = $customer->getId();
}

$_groups = array();
$groupCollection = Mage::getResourceModel('customer/group_collection');
foreach ($groupCollection as $group) {
    $_groups[$group->getId()] = $group->getCode();
}

foreach ($_customer as $groupId => $customerIds) {
    echo 'Group "' . $_groups[$groupId] . '" has ' . count($customerIds) . ' customers.<br>';
}
2

You have to initiate Mage first. Add to top of your file ...

require_once('app/Mage.php');
Mage::app();

Edit

Please try this ...

$_customer = array();
$customerCollection = Mage::getResourceModel('customer/customer_collection');
foreach ($customerCollection as $customer) {
    $_customer[$customer->getGroupId()][] = $customer->getId();
}

$_groups = array();
$groupCollection = Mage::getResourceModel('customer/group_collection');
foreach ($groupCollection as $group) {
    $_groups[$group->getId()] = $group->getCode();
}

foreach ($_customer as $groupId => $customerIds) {
    echo 'Group "' . $_groups[$groupId] . '" has ' . count($customerIds) . ' customers.<br>';
}

Maybe not best-practice. This can be done in one collection using joinLeft on customer group table ...

  • Thank you for your interest. But now I get Fatal error: Call to a member function addField() on a non-object in /domains/dev.vampirevape.co.uk/http/customer_groups.php on line 19 – Greg Oct 24 '17 at 16:09
  • @BareFeet Updated answer. You add field code belongs to Magentos admin forms and not to standalone scripts. Either add this to a custom admin grid/from our use some simple html output for your script. – sv3n Oct 24 '17 at 16:21
  • Wow, that gave me a lot of results... Pages and pages long strings. Have a look at my question, this is what I want to achieve but it's not going well for me. – Greg Oct 24 '17 at 16:27
  • @BareFeet its not really clear what you want at the end. A customergroup dropdown? Where to display? Or just a list of customers that belong to given group? – sv3n Oct 24 '17 at 16:30
  • What I want, is a way of finding out how many customers are in the given groups. It's just for me to find out which groups are being used the most as I want to decrease the amount of groups. This would help me determinate which ones. – Greg Oct 24 '17 at 16:35

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.