【发布时间】:2012-12-06 11:59:45
【问题描述】:
我想知道如何在 magento 1.7 的订单网格中添加以下两个额外的列
- 客户总订单数
- 客户在订单上花费的总金额
我已设法添加列,但我无法让它显示任何数据。我相信问题的关键在于函数 *_prepareCollection()*。
【问题讨论】:
标签: magento
我想知道如何在 magento 1.7 的订单网格中添加以下两个额外的列
我已设法添加列,但我无法让它显示任何数据。我相信问题的关键在于函数 *_prepareCollection()*。
【问题讨论】:
标签: magento
你是对的,Magento 中任何网格的内容都包含在集合中。
集合对象最终会解析为 MySQL 查询,因此为了让它们显示在网格中,您需要将该数据加入集合中。只要您要求它们是可搜索和可排序的。
您可以通过将子选择加入您的集合来完成此操作,例如:
$alias = 'subselect';
$subselect = Mage::getModel('Varien_Db_Select',
Mage::getSingleton('core/resource')->getConnection('core_read')
)->from('sales_flat_order_grid', array(
'customer_id as s_customer_id',
'sum(grand_total) as total_revenue',
'count(*) as total_orders')
)->group('customer_id');
$collection->getSelect()
->joinInner(array($alias => $subselect),
"{$alias}.s_customer_id = main_table.customer_id");
在您的_prepareCollection() 调用中,它应该覆盖app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php 中的调用。
然后您可以在您的_prepareColumns() 函数中定义total_revenue 和total_orders 的列。
实现此目的的另一种方法是关闭搜索和排序并在列上调用渲染器,然后实例化客户模型并将所有内容即时组合在一起。
【讨论】:
抱歉需要 50 个代表发表评论(我认为),但乔纳森指的是(我希望)该文件:
/app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php
你应该扩展哪个(所以不要编辑核心),将文件复制到:
/app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php
并在那里编辑建议的 _prepareCollection() 和 _prepareColumns()
在调用 setCollection() 之前将其添加到 _prepareCollection() 函数中:
the answer from Jonathan
这个到你的 _prepareColumns()
$this->addColumn('total_revenue', array(
'header' => Mage::helper('sales')->__('Total Revenue'),
'index' => 'total_revenue',
'filter_index' => 'ctotals.total_revenue',
));
$this->addColumn('total_orders', array(
'header' => Mage::helper('sales')->__('Total Orders'),
'index' => 'total_orders',
'filter_index' => 'ctotals.total_orders',
));
【讨论】: