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()
    ->columns(array('orders_count' => new Zend_Db_Expr('COUNT(*)'))
    array('orders_count' => new Zend_Db_Expr('('.$select.')')),
    'e.entity_id = orders_count.customer_id',
$collection->addFilterToMap('orders_count', 'orders_count.orders_count');

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/… Dec 6, 2017 at 15:05
  • Which step did not I follow ?
    – Kozame
    Dec 6, 2017 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, 2017 at 15:26
  • did you create _ordersCountFilterCallBack function? Dec 6, 2017 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, 2017 at 15:32

1 Answer 1


You can try below.

$select = $readAdapter->select()
    ->columns(array('orders_count' => new Zend_Db_Expr('IFNULL(COUNT(*),0)'))

I have not tested it but it should work.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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