Magento Sales Order Grid shows the wrong number of records when adding Skus names and columns

I work with Magento version 1.4, and I added additional grid tables (names and skus) to the customer order grid, the returned data is correct, but I have problems with pagination and the total number of records, my code is as follows

First I edited Mage_Adminhtml_Block_Sales_Order_Grid

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->_getCollectionClass())
    ->join(
        'sales/order_item',
        '`sales/order_item`.order_id=`main_table`.entity_id',
        array(
            'skus'  => new Zend_Db_Expr('group_concat(`sales/order_item`.sku SEPARATOR ", ")'),
            'names' => new Zend_Db_Expr('group_concat(`sales/order_item`.name SEPARATOR ", ")'),
            )
        );
    $collection->getSelect()->group('entity_id');

    $this->setCollection($collection);
    return parent::_prepareCollection();
}

Then I override this method to return the correct results when filtering by name or skus

    protected function _addColumnFilterToCollection($column)
{
    if($this->getCollection() && $column->getFilter()->getValue()) 
    {
        if($column->getId() == 'skus'){
            $this->getCollection()->join(
                'sales/order_item',
                '`sales/order_item`.order_id=`main_table`.entity_id',
                array(
                    'skus'  => new Zend_Db_Expr('group_concat(`sales/order_item`.sku SEPARATOR ", ")'),
                )
            )->getSelect()
                ->having('find_in_set(?, skus)', $column->getFilter()->getValue());

            return $this;
        }

        if($column->getId() == 'names'){
            $this->getCollection()->join(
                'sales/order_item',
                '`sales/order_item`.order_id=`main_table`.entity_id',
                array(
                    'names' => new Zend_Db_Expr('group_concat(`sales/order_item`.name SEPARATOR ", ")'),
                )
            )->getSelect()
                ->having('find_in_set(?, names)', $column->getFilter()->getValue());

            return $this;
        }
    }
    return parent::_addColumnFilterToCollection($column);
}

Then I edited this getSelectCountSql () method in the Mage_Sales_Model_Mysql4_Order_Collection class

public function getSelectCountSql()
{
    $countSelect = parent::getSelectCountSql();

    //added 
    $countSelect->reset(Zend_Db_Select::HAVING);
    //end

    $countSelect->resetJoinLeft();
    return $countSelect;
}

Any idea how I can calculate the number of rows? Thanks at Advance.

+5
source share
3 answers

, , GROUP insted HAVING:

$countSelect->reset(Zend_Db_Select::GROUP);

:

$collection->getSelect()->group('entity_id');
+4
$collection->getSelect()->join(array(
            'item'=>$collection->getTable('sales/order_item')),
            'item.order_id=`main_table`.entity_id AND item.product_type="simple"',
            array(
                'skus' => new Zend_Db_Expr('group_concat(item.sku SEPARATOR ", ")'),
                'name' => new Zend_Db_Expr('group_concat(item.name SEPARATOR ", ")')
            ));

$this->addColumn('skus', array(
            'header' => Mage::helper('sales')->__('SKU'),
            'index' => 'skus',
            'type' => 'text',
        ));

        $this->addColumn('name', array(
            'header' => Mage::helper('sales')->__('NAME'),
            'index' => 'name',
            'type' => 'text'
        ));
+2

I had this problem and it worked for me, implementing the custom function getSize () in the collection that I use

public function getSize()
{
    $select = clone $this->getSelect();
    $select->reset();
    $select =  $this->getConnection()->fetchOne('SELECT COUNT(*) FROM Table GROUP BY FIELD'); // or you can use select count(distinct field) from table
    return $select;
}

and to save the grid storage I redefine

protected function _setCollectionOrder($column)
    {
        $collection = $this->getCollection();
        if ($collection) {
            $columnIndex = $column->getFilterIndex() ?
                $column->getFilterIndex() : $column->getIndex();
            $collection->getSelect()->order(array($columnIndex.' '.$column->getDir()));
        }
        return $this;
    }

and set filter_index columns TO

 in _prepareColumns() function 
    'filter_index' => 'SUM(tablename.field)'

and you can use the callback function for filters for columns

0
source

All Articles