2013-09-23 53 views
1

我要顯示在Magento如何添加訂單客戶電網的總人數在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

你有一個GROUP BY條款您的收藏中,與電網分頁器$collection->getSize()確定的頁數。問題是getSize()SELECT COUNT(*)應用於集合,並獲取第一行的第一列以獲取結果數。隨着GROUP BY仍然應用,尋呼機然後認爲只有一個結果。

要避免此問題,您應該使用您自己的客戶收集與相關的getSize(),或使用子查詢檢索您需要的總計。

+0

我明白這個問題,但我怎麼能覆蓋的getSize功能? – user2807370

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'); 
0

它在那裏工作得很好。只需按照以下步驟。

在以下文件中添加代碼 app \ code \ core \ Mage \ Adminhtml \ Block \ Customer \ 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, 
     )); 
相關問題