【问题标题】:how to add total number of orders to customer grid in magento如何在magento中将订单总数添加到客户网格
【发布时间】:2013-09-28 10:08:52
【问题描述】:

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

我以此为指导: How to add customer "total number of orders" and "total spent" to order grid in 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+)

  • 对此新列的排序不起作用

【问题讨论】:

    标签: php magento


    【解决方案1】:

    只需删除:

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

    【讨论】:

      【解决方案2】:

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

      为防止出现此问题,您应该使用您自己的客户集合和相关的getSize(),或者使用子查询来检索您需要的总数。

      【讨论】:

      • 我明白这个问题,但我怎样才能覆盖 getSize 函数?
      【解决方案3】:

      那里工作得很好。只需按照以下步骤操作即可。

      在以下文件中添加代码 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,
              ));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多