magento admin产品网格添加类别过滤器到管理网格而不添加列

时间:2013-06-18 13:34:53

标签: magento magento-1.7

我正在尝试按类别过滤网格而不添加类别列,是否有一种方法可以将此自定义过滤器用于网格。

下面是我做的

function prepareCollection()
{
$catIdArray = getRequest()->getParam('cat_id');
//i.e  $catIdArray = array(26, 27, 17);

$collection = Mage::getModel('catalog/product')->getCollection(); 

$collection->joinField(
    'category_id',
    'catalog/category_product',
    'category_id',
    'product_id=entity_id',
    null,
    'left'
)
->addAttributeToFilter( 
    'category_id',
    array('in' => $catIdArray)
)
->addAttributeToSelect('*');
$this->setCollection($collection);

此处过滤器工作正常,但在这种情况下分页不起作用。请建议我,我做错了什么????

1 个答案:

答案 0 :(得分:1)

为了在扩展Magento后台的网格后修复分页,你需要扩展 lib / Varien / Data / Collection / Db.php

Db.php 复制到路径 app / code / local / Varien / Data / Collection / Db.php ,并将函数getSelectCountSql替换为下面: -

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;
}