我想显示magento客户网格中的订单数量
我用这个作为指南: How to add customer "total number of orders" and "total spent" to order grid in magento 1.7
但这是一个不同的网格
到目前为止我创建了: 应用程序/代码/本地/法师/ Adminhtml /砌块/客户/ Grid.php
_prepareCollection
我补充说:
$orderTableName = Mage::getSingleton('core/resource')
->getTableName('sales/order');
$collection
->getSelect()
->joinLeft(
array('orders' => $orderTableName),
'orders.customer_id=e.entity_id',
array('order_count' => 'COUNT(customer_id)')
);
$collection->groupByAttribute('entity_id');
之前: $这 - > setCollection($集合);
我添加了_prepareColumns:
$this->addColumn('order_count', array(
'header' => Mage::helper('customer')->__('# orders'),
'index' => 'order_count',
'type' => 'number'
));
虽然它在网格中工作,但我遇到了一些问题:
寻呼机显示1位客户(应为500 +)
对此新列进行排序不起作用
答案 0 :(得分:0)
您的集合中有GROUP BY
子句,并且网格寻呼机使用$collection->getSize()
来确定页数。问题是getSize()
将SELECT COUNT(*)
应用于集合,并获取第一行的第一列以获取结果数。在GROUP BY
仍然应用的情况下,寻呼机会认为只有一个结果。
要防止出现此问题,您应该将自己的客户集合与相关的getSize()
一起使用,或者使用子查询来检索所需的总计。
答案 1 :(得分:0)
只需删除:
$collection->groupByAttribute('entity_id');
并添加:
$collection->group('e.entity_id');
概述我们:
$orderTableName = Mage::getSingleton('core/resource')
->getTableName('sales/order');
$collection
->getSelect()
->joinLeft(
array('orders' => $orderTableName),
'orders.customer_id=e.entity_id',
array('order_count' => 'COUNT(customer_id)')
);
$collection->group('e.entity_id');
OR
$orderTableName = Mage::getSingleton('core/resource')
->getTableName('sales/order');
$collection
->getSelect()
->joinLeft(
array('orders' => $orderTableName),
'orders.customer_id=e.entity_id',
array('order_count' => 'COUNT(customer_id)')
)
->group('e.entity_id');
答案 2 :(得分:0)
那里的工作正常。只需按照以下步骤操作即可。
在以下文件中添加代码 应用\代码\核心\法师\ Adminhtml \块\客户\ Grid.php
add this code in _prepareCollection() fucntion only
$sql ='SELECT COUNT(*)'
. ' FROM ' . Mage::getSingleton('core/resource')->getTableName('sales/order') . ' AS o'
. ' WHERE o.customer_id = e.entity_id ';
$expr = new Zend_Db_Expr('(' . $sql . ')');
$collection->getSelect()->from(null, array('orders_count'=>$expr));
并在_prepareColumns()函数中添加此代码并使用相同的文件
$this->addColumn('orders_count', array(
'header' => Mage::helper('customer')->__('Total Orders'),
'align' => 'left',
'width' => '40px',
'index' => 'orders_count',
'type' => 'number',
'sortable' => true,
));