Magento / PHP - 如何获取客户群中所有成员的数量

时间:2015-06-01 17:24:54

标签: php magento

我正在开发自定义magento扩展程序。

我已经为所有客户群制作了一个选择菜单,以下是代码:

$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
        )
    );

结果如下:

enter image description here

如您所见,结果如下:

  • 未登录
  • 一般
  • 批发
  • 零售商

我想要的只是添加每个客户群中的成员数量,例如General - (125 members)

让我举一个例子,说明我想要实现的目标,但不知道如何形象:

enter image description here

你能帮助我实现这个目标吗?

提前致谢!

3 个答案:

答案 0 :(得分:0)

此查询应该完成这项工作:

SELECT `group_id`, count(*) AS `count` FROM `customer_entity` GROUP BY `group_id`

答案 1 :(得分:0)

如果您不想在数据库上使用直接MySQL查询,那么您可以尝试这样做:

$customerCollectionSize = array();
$customerGroups         = Mage::getModel('customer/group')->getCollection();

foreach($customerGroups as $customerGroup) {
  $customerGroupId    = $customerGroup->getId();
  $customerCollection = Mage::getModel('customer/customer')
        ->getCollection()
        ->addAttributeToSelect('*')
        ->addFieldToFilter('group_id', $customerGroupId);

        $customerCollectionSize[$customerGroupId] = $customerCollection->getSize(); 
}

现在在这个$ customerCollectionSize数组中你将获得总数。对应于客户组ID的客户。

如果您想使用直接MySQL查询,请尝试上面提到的@ tom-lankhorst:

SELECT `group_id`, count(*) AS `count` FROM `customer_entity` GROUP BY `group_id`

希望这会有所帮助!!

答案 2 :(得分:-1)

按客户组名称分组:

  SELECT g.customer_group_code, count(e.entity_id) AS `count` FROM `customer_entity` AS e
    JOIN customer_group AS g on g.customer_group_id = e.group_id
    GROUP BY 1