Magento1.7の注文グリッドに次の2つの列を追加する方法を知りたいです。
- 顧客の合計注文数
- 注文に費やされた顧客の合計金額
カラムを追加できましたが、データを表示できません。問題の鍵は関数*_prepareCollection()*にあると思います。
Magento1.7の注文グリッドに次の2つの列を追加する方法を知りたいです。
カラムを追加できましたが、データを表示できません。問題の鍵は関数*_prepareCollection()*にあると思います。
そうです、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
。
次に、関数で列total_revenue
と列を定義できます。total_orders
_prepareColumns()
これを実現するもう 1 つの方法は、検索と並べ替えをオフにして、列でレンダラーを呼び出し、顧客モデルをインスタンス化して、すべてをその場でまとめることです。
申し訳ありませんが、コメントするには 50 人の担当者が必要ですが (私はそう思います)、Jonathan は次のファイルを参照しています (希望します)。
/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',
));