如何将订单总数添加到洋红色中的客户网格


how to add total number of orders to customer grid in magento

我想在Magento的客户网格中显示订单数量

我以此为指导:如何将客户"订单总数"和"总花费"添加到Magento 1.7中的订单网格

但这是一个不同的网格

到目前为止,我已经创建了:app/code/local/Mage/Adminhtml/Block/Customer/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');

以前:$this->setCollection($collection);

_prepareColumns我补充说:

$this->addColumn('order_count', array(
            'header'    => Mage::helper('customer')->__('# orders'),
            'index'     => 'order_count',
             'type'  => 'number'
        ));

虽然它确实在网格中工作,但我有一些问题:

  • 寻呼机显示 1 个客户(应为 500+)

  • 在此新列上排序不起作用

只需删除:

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

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

您的集合中有一个 GROUP BY 子句,网格页导航器使用 $collection->getSize() 来确定页数。问题是getSize()对集合应用SELECT COUNT(*),并获取第一行的第一列以获取结果数。在仍然应用GROUP BY的情况下,寻呼机会认为只有一个结果。

若要防止此问题,应将自己的客户集合与相关getSize()一起使用,或使用子查询来检索所需的总计。

那边工作正常。 只需按照以下步骤操作即可。

在以下文件中添加代码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,
        ));
We can create a column of total orders in customer grid table and display in numbers .
Vendor/Module/view/adminhtml/ui_component/customerlisting.xml
<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
   <columns name="customer_columns" class="Magento'Customer'Ui'Component'Listing'Columns" >
       <column name="total_orders" class="Vendor'Module'Ui'Component'Listing'Column'TotalOrders" sortOrder="90">
           <settings>
               <dataType>text</dataType>
               <label translate="true">Total Orders</label>
               <sortable>false</sortable>
               <filter>false</filter>
           </settings>
       </column>
   </columns>
</listing>
And then create a Ui component to fetch orderdata.
Vendor/Module/Ui/Component/Listing/Column/TotalOrders.php
<?php
namespace Vendor'Module'Ui'Component'Listing'Column;
use Magento'Framework'View'Element'UiComponent'ContextInterface;
use Magento'Framework'View'Element'UiComponentFactory;
use Magento'Ui'Component'Listing'Columns'Column;
class TotalOrders extends Column
{
   protected $orderCollectionFactory;
   /**
    * 
    * @param ContextInterface   $context           
    * @param UiComponentFactory $uiComponentFactory   
    * @param array              $components        
    * @param array              $data              
    */
   public function __construct(
       ContextInterface $context,
       UiComponentFactory $uiComponentFactory,
       array $components = [],
       array $data = [],
       'Magento'Sales'Model'ResourceModel'Order'CollectionFactory  $orderCollectionFactory
   ) {
       $this->orderCollectionFactory = $orderCollectionFactory;
       parent::__construct($context, $uiComponentFactory, $components, $data);
   }
   /**
    * Prepare Data Source
    *
    * @param array $dataSource
    * @return array
    */
   public function prepareDataSource(array $dataSource)
   {
       if (isset($dataSource['data']['items'])) {
           foreach ($dataSource['data']['items'] as & $item) {
               $customerOrder = $this->orderCollectionFactory->create()->addFieldToFilter('customer_id', $item['entity_id']);
               $item[$this->getData('name')] = count($customerOrder);//Value which you want to display**strong text**
           }
       }
       return $dataSource;
   }
}