protected function _prepareCollection() {
$this->setDefaultLimit(200);
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->joinLeft(array('d' => 'customer_entity'), 'main_table.customer_id = d.entity_id', array('d.group_id'));
$collection->getSelect()->joinLeft(array('c' => 'customer_group'), 'd.group_id = c.customer_group_id', array('c.customer_group_code'));
$collection->getSelect()->joinLeft(array('f' => 'sales_flat_shipment_track'), 'main_table.entity_id = f.order_id', array('f.track_number'));
$collection->getSelect()->joinLeft('sales_flat_order_address', 'main_table.entity_id = sales_flat_order_address.parent_id', array('telephone', 'city', 'postcode', 'country_id'))->where("sales_flat_order_address.address_type = 'billing'");
$collection->getSelect()->reset(Zend_Db_Select::GROUP);
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns() {
$this->addColumn('real_order_id', array(
'header' => Mage::helper('sales')->__('Order #'),
'width' => '80px',
'type' => 'text',
'index' => 'increment_id',
'filter_index' => 'main_table.increment_id',
));
if (!Mage::app()->isSingleStoreMode()) {
$this->addColumn('store_id', array(
'header' => Mage::helper('sales')->__('Purchased From (Store)'),
'index' => 'store_id',
'type' => 'store',
'store_view' => true,
'display_deleted' => true,
));
}
$this->addColumn('customer_group_code', array(
'header' => Mage::helper('sales')->__('Customer Group Id'),
'index' => 'customer_group_code',
'type' => 'text',
'filter_index' => 'c.customer_group_code',
));
$this->addColumn('track_number', array(
'header' => Mage::helper('sales')->__('Track Number'),
'index' => 'track_number',
'type' => 'text',
'filter_index' => 'f.track_number'
));
<pre name="code" class="php">class namespace_module_Model_Resource_Order_Collection
extends Mage_Sales_Model_Resource_Order_Collection
{
public function getSelectCountSql()
{
$this->_renderFilters();
$countSelect = clone $this->getSelect();
$countSelect->reset(Zend_Db_Select::ORDER);
$countSelect->reset(Zend_Db_Select::LIMIT_COUNT);
$countSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
$countSelect->reset(Zend_Db_Select::COLUMNS);
// Count doesn't work with group by columns keep the group by
if(count($this->getSelect()->getPart(Zend_Db_Select::GROUP)) > 0) {
$countSelect->reset(Zend_Db_Select::GROUP);
$countSelect->distinct(true);
$group = $this->getSelect()->getPart(Zend_Db_Select::GROUP);
$countSelect->columns("COUNT(DISTINCT ".implode(", ", $group).")");
} else {
$countSelect->columns('COUNT(*)');
}
return $countSelect;
}
}