本文介绍了Magento 销售订单网格在添加名称和 Skus 列时显示不正确的记录数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Magento 1.4 版,我在销售订单网格中添加了额外的网格列(名称和 skus),返回的数据是正确的,但我遇到了分页和记录总数问题,我的代码如下:

I'm working with Magento version 1.4 and I added extra grid columns (names and skus) to Sales Order Grid, the returned data is correct but I'm having problems with pagination and total number of records, my code as follow:

首先我编辑了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();
}

然后我重写此方法以在按名称或 skus 过滤时返回正确的结果

Then I override this method to return correct results when filter by names 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);
}

然后我在 Mage_Sales_Model_Mysql4_Order_Collection 类中编辑了这个方法 getSelectCountSql():

Then I edited this method getSelectCountSql() in 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;
}

如何计算行数?

推荐答案

可能有点晚了,但在你的代码中尝试使用 GROUP insted of HAVING:

Maybe its a bit to late but in your code try using GROUP insted of HAVING:

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

因为你在使用这个声明:

Because you are using this statemen:

$collection->getSelect()->group('entity_id');

这篇关于Magento 销售订单网格在添加名称和 Skus 列时显示不正确的记录数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-31 17:21