0

I want to display the total of order for each customer, in the customer grid (Back Office).

Currently, I succeed to display the total, only if there is at least 1 order.

In _prepareCollection() :

$readAdapter = Mage::getSingleton('core/resource')->getConnection('core_read');
$select = $readAdapter->select()
    ->from('sales_flat_order_grid')
    ->columns(array('orders_count' => new Zend_Db_Expr('COUNT(*)'))
        )->group('customer_id');
$collection->getSelect()->joinLeft(
    array('orders_count' => new Zend_Db_Expr('('.$select.')')),
    'e.entity_id = orders_count.customer_id',
    array('orders_count.orders_count')
);
$collection->addFilterToMap('orders_count', 'orders_count.orders_count');
$this->setCollection($collection);

In _prepareColumns(), I added the custom column :

$this->addColumn('orders_count', array(
    'header'    => Mage::helper('customer')->__('Total nb of orders'),
    'width'     => '40px',
    'index'     => 'orders_count',
    'type'      => 'number',
    'sortable'  => false,
    'filter_condition_callback' => array($this, '_ordersCountFilterCallBack')
));

I can force the displaying of 0 by add 'default' => '0' in my column but it's not that I want : In my _ordersCountFilterCallBack function, I want to be allow to filter orders_count.orders_count == 0 and not orders_count.orders_count IS NULL.

Any idea ?

  • You copied some code from this post but not all. Please follow all the steps it should work magento.stackexchange.com/questions/162903/… – Abhishek Panchal Dec 6 '17 at 15:05
  • Which step did not I follow ? – Kozame Dec 6 '17 at 15:19
  • The only line I didn't add is $collection->addFilterToMap('orders_count', 'orders_count.orders_count'); because I don't know this purpose and with this line ou without, the result is the same for me. – Kozame Dec 6 '17 at 15:26
  • did you create _ordersCountFilterCallBack function? – Abhishek Panchal Dec 6 '17 at 15:27
  • Yes and the filtrer work for all orders_count.orders_count >=1. But on the grid, that not display '0' for customer wihtout order and on my filter, I have to handle "orders_count.orders_count IS NULL" instead of "orders_count.orders_count == 0 ". :/ – Kozame Dec 6 '17 at 15:32
0

You can try below.

$select = $readAdapter->select()
    ->from('sales_flat_order_grid')
    ->columns(array('orders_count' => new Zend_Db_Expr('IFNULL(COUNT(*),0)'))
        )->group('customer_id');

I have not tested it but it should work.

  • That seems logic, but that not works for me :( – Kozame Dec 7 '17 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.