Magento 1.7 – Fix Pagination Broken by Custom Collection

collection;magento-1.7

Below is the code for creating a custom collection, but it is breaking pagination. It is showing only one item in pagination, When I am printing getSize() it is giving one(which is wrong), but on printing count() it is printing correct result.

               $sampleqry = 'select entity_id from catalog_product_entity_int as sample where sample.store_id=0 and sample.attribute_id="169" and sample.value=0';

                $this->_productCollection->getSelect()->joinLeft(array('r' => 'catalog_product_relation'), 'r.parent_id=e.entity_id', array());
                $this->_productCollection->getSelect()->joinLeft(array('s' => 'sales_flat_order_item'), '(r.child_id=s.product_id AND s.product_id IN(' . $sampleqry . ')) OR (s.product_id=e.entity_id)', array('SUM(s.qty_ordered) as qtyordered'));
                $this->_productCollection->getSelect()->group('e.entity_id');
                $this->_productCollection->getSelect()->order('qtyordered DESC');

Same code is working fine in Magento 1.4 but not in 1.7.0.2
How can I fix this issue, any help will be appreciated.

Best Answer

when you apply group on collection magento collection counts gets break, this i had fixed by using below code.

app/code/local/Mage/Catalog/Model/Resource/Product/Collection.php

protected function _getSelectCountSql($select = null, $resetLeftJoins = true)
    {
       $this->_renderFilters();
       $countSelect = (is_null($select)) ?
           $this->_getClearSelect() :
           $this->_buildClearSelect($select);


       if(count($countSelect->getPart(Zend_Db_Select::GROUP)) > 0) {
           $countSelect->reset(Zend_Db_Select::GROUP);
       }


       $countSelect->columns('COUNT(DISTINCT e.entity_id)');
       if ($resetLeftJoins) {
           $countSelect->resetJoinLeft();
       }
       return $countSelect;
    }
Related Topic