1

Brief

Add a column to the Order Grid, entitled "Total Orders" that shows the total count of orders for each customer.

Problem

I've taken what I've understood from here. This has successfully managed to create the desired column but without any results. I believe the error lies somewhere within part 2 of my implementation.

Where I'm at

1 - Override created /app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php

2 - Added below to _prepareCollection()

$alias = 'subselect';
$subselect = Mage::getModel('Varien_Db_Select', 
    Mage::getSingleton('core/resource')->getConnection('core_read')
    )->from('sales_flat_order_grid', array(
            'customer_id as s_customer_id', 
            'count(*) as total_orders')
    )->group('customer_id');

$collection->getSelect()
    ->joinInner(array($alias => $subselect), 
        "{$alias}.s_customer_id = main_table.customer_id");

3 - Added below to _prepareColumns()

$this->addColumn('total_orders', array(
    'header' => Mage::helper('sales')->__('Total Orders'),
    'index' => 'total_orders',
    'filter_index' => 'total_orders',
));
1
1

The one thing stopping this from working was my positioning of the code in Step 2.

I tried added my code in step 2 after the line containing return parent::_prepareCollection();

This is how step 2 should look in its entirety

    protected function _prepareCollection()
    {

    $collection = Mage::getResourceModel($this->_getCollectionClass());
    $this->setCollection($collection);

    //Added prepare for Total Orders
    $alias = 'subselect';
    $subselect = Mage::getModel('Varien_Db_Select', 
        Mage::getSingleton('core/resource')->getConnection('core_read')
        )->from('sales_flat_order_grid', array(
                'customer_id as s_customer_id', 
                'count(*) as total_orders')
        )->group('customer_id');

    $collection->getSelect()
        ->joinInner(array($alias => $subselect), 
            "{$alias}.s_customer_id = main_table.customer_id");
    //Added prepare for Total Orders

    return parent::_prepareCollection();

    }
1
  • This will not work on guest orders, since there is no customer_id listed for a guest order. – ProxiBlue Jul 24 '18 at 22:15

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.