Question

When I go to some categories on my site and click through to the end of the pagination, it is adding additional pages with the same products. For the categories that this is happening on, I have set the "Products Per Page Allowed Values" to 26 under Category > Display Settings.

For example, in the screen capture below you can see that it should end on Page 13, but it is adding two additional pages.

screen capture taken on my local site

However, if I remove the 26 from the category settings and just go with the default set at store level, which is 24, I do not have this issue.

Any suggestions on what could be causing this? I think I need to do something with pager.phtml and/or pager.php, but I am not exactly sure what.

I have modified the list.phtml, so it will take into account some featured item and aside functionality that is utilized on our site. It looks like this:

$_productCollection=$this->getLoadedProductCollection();
$_productCollection->clear(); // clear loaded collection to be able to get new collection when checking for featured item and aside

$_clsCustomHelper = $this->helper('cls_custom');
$_helper = $this->helper('catalog/output');
$_asideHtml = Mage::helper('cls_custom/category')->getAsideBlockHtml($this->getParentBlock()->getCurrentCategory());
$_mobileAsideHtml = Mage::helper('cls_custom/category')->getMobileAsideBlockHtml($this->getParentBlock()->getCurrentCategory());

$current_page = Mage::getBlockSingleton('page/html_pager')->getCurrentPage();

if(Mage::registry('current_category')->getHasImageGallery()) {
    $_clsCustomHelper->attachImageGalleryToCollection($_productCollection);
}

$topParent = $this->getParentBlock()->getCurrentCategory();
$path = $topParent->getPath();
$ids = explode('/', $path);
if ( isset( $ids[2]) ) {
    $topParent = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($ids[2]);
}

$is_card = ( (bool) $topParent->getData('is_card') );

$currentCategory = Mage::registry('current_category');
// get number of items that should appear on page by category
$thisPageSize = $currentCategory->getData('per_page_values');
// if number of items is not set at category level, get count set at store level
$usesStoreSize = null;
if(!$thisPageSize) {
    $thisPageSize = Mage::getStoreConfig('catalog/frontend/grid_per_page');
    $usesStoreSize = 't';
}
$_asideHtml = Mage::helper('cls_custom/category')->getAsideBlockHtml($this->getParentBlock()->getCurrentCategory());
// check to see if category has featured item(s)
$_collectionFeaturedItem = $this->helper('Dayspring_Catalog')->hasFeaturedItem($currentCategory);
$_hasFeaturedItem = null;
// if category has featured item(s), loop through and get the item's position
// set that category does have a featured item
if($_collectionFeaturedItem){
    foreach ($_collectionFeaturedItem as $product) {
        if ($product->getFeaturedItem()) {  
            if($_itemPositions) {
                $_itemPositions = $_itemPositions . ';' .$product->getCatIndexPosition();
            } else {
                $_itemPositions = $product->getCatIndexPosition();
            }
            $_hasFeaturedItem = 'yes';
        }
    }
}
// get the range of items to display based on current page and number of items to appear
$endCount = $thisPageSize*$current_page;
$startCount = ($endCount-$thisPageSize)+1;
// if has position items, loop through and based on position determine if displaying on page or not
$_itemPositions = explode(';',$_itemPositions);
$_onPage = null;
foreach($_itemPositions as $pos){
    $pos = trim($pos);
    if(($pos > $startCount) && ($pos < $endCount)){
        $_onPage = 't';
    }
}
// if page has aside and featured item
if($_asideHtml && $_hasFeaturedItem) {
    if($current_page == 1) {
        if($usesStoreSize == 't') {
            $_productCollection->setPageSize($thisPageSize+3)->setCurPage($current_page);
        } else {
            $_productCollection->setPageSize($thisPageSize+1)->setCurPage($current_page);
        }
    } else {
        if($usesStoreSize == 't') {
            $_productCollection->setPageSize($thisPageSize)->setCurPage($current_page);
        } else {
            $_productCollection->setPageSize($thisPageSize+2)->setCurPage($current_page);
        }
    }
// if page has aside only
}elseif($_asideHtml) {
    if($current_page == 1) {
        if($usesStoreSize == 't') {
            $_productCollection->setPageSize($thisPageSize+2)->setCurPage($current_page);
        } else {
            $_productCollection->setPageSize($thisPageSize)->setCurPage($current_page);
        }
    } else {
        if($usesStoreSize == 't') {
            $_productCollection->setPageSize($thisPageSize)->setCurPage($current_page);
        } else {
            $_productCollection->setPageSize($thisPageSize+2)->setCurPage($current_page);
        }

    }
// if page has featured item only
}elseif($_hasFeaturedItem){
    if($current_page == 1) {
        if($usesStoreSize == 't') {
            $_productCollection->setPageSize($thisPageSize+1)->setCurPage($current_page);
        } else {
            $_productCollection->setPageSize($thisPageSize+3)->setCurPage($current_page);
        }
    }else{
        if($usesStoreSize == 't') {
            if($_onPage == 't'){
                $_productCollection->setPageSize($thisPageSize+1)->setCurPage($current_page);
            }else{
                $_productCollection->setPageSize($thisPageSize)->setCurPage($current_page);    
            }
        } else {
            $_productCollection->setPageSize($thisPageSize+2)->setCurPage($current_page);
        }
    }
}

I have added a new version of Toolbar.php in my local folder, to adjust for a sorting issue we had ... it looks like this:

class DS_Catalog_Block_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar
{

/**
 * Set collection to pager
 *
 * @param Varien_Data_Collection $collection
 * @return Mage_Catalog_Block_Product_List_Toolbar
 */
public function setCollection($collection)
{
    $this->_collection = $collection;

    $this->_collection->setCurPage($this->getCurrentPage());

    // we need to set pagination only if passed value integer and more that 0
    $limit = (int)$this->getLimit();
    if ($limit) {
        $this->_collection->setPageSize($limit);
    }
    if ($this->getCurrentOrder()) {
        if(($this->getCurrentOrder())=='position'){ //defines the sort option
            //sort by position (ascending) and entity_id (descending)
            $this->_collection->addAttributeToSort('position','asc');
        } else {
            $this->_collection->setOrder($this->getCurrentOrder(),$this->getCurrentDirection());
        }
    }
    return $this;
}
}
Was it helpful?

Solution

Ended up figuring out where exactly I needed to modify the code. It did have to do with updating the pager in some sense.

In my custom list.php, I modified the the _beforeToHtml function:

protected function _beforeToHtml()
{ 

    $toolbarCategory = Mage::registry('current_category');
    // get number of items that should appear on page by category
    $toolbarPageSize = $toolbarCategory->getData('per_page_values');
    // if number of items is not set at category level, get count set at store level
    $toolbarUsesStoreSize = null;
    if(!$toolbarPageSize) {
        $toolbarPageSize = Mage::getStoreConfig('catalog/frontend/grid_per_page');
        $toolbarUsesStoreSize = 't';
    }
    $toolbarAsideHtml = Mage::helper('cls_custom/category')->getAsideBlockHtml($this->getParentBlock()->getCurrentCategory());
    // check to see if category has featured item(s)
    $toolbarCollectionFeaturedItem = $this->helper('Dayspring_Catalog')->hasFeaturedItem($toolbarCategory);
    $toolbarHasFeaturedItem = null;
    // if category has featured item(s), loop through and get the item's position
    // set that category does have a featured item
    if($toolbarCollectionFeaturedItem){
        foreach ($toolbarCollectionFeaturedItem as $product) {
            if ($product->getFeaturedItem()) {  
                if($toolbarItemPositions) {
                    $toolbarItemPositions = $toolbarItemPositions . ';' .$product->getCatIndexPosition();
                } else {
                    $toolbarItemPositions = $product->getCatIndexPosition();
                }
                $toolbarHasFeaturedItem = 'yes';
            }
        }
    }
    // get the range of items to display based on current page and number of items to appear
    $toolbarEndCount = $toolbarPageSize*$current_page;
    $toolbarStartCount = ($toolbarEndCount-$toolbarPageSize)+1;
    // if has position items, loop through and based on position determine if displaying on page or not
    $toolbarItemPositions = explode(';',$toolbarItemPositions);
    $toolbarOnPage = null;
    foreach($toolbarItemPositions as $pos){
        $pos = trim($pos);
        if(($pos > $toolbarStartCount) && ($pos < $toolbarEndCount)){
            $toolbarOnPage = 't';
        }
    }
    // if page has aside and featured item
    if($toolbarAsideHtml && $toolbarHasFeaturedItem) {
        if($current_page == 1) {
            if($toolbarUsesStoreSize == 't') {
                $customPageLimit = $toolbarPageSize;
            } else {
                $customPageLimit = ($toolbarPageSize+1);
            }
        } else {
            if($toolbarUsesStoreSize == 't') {
                $customPageLimit = $toolbarPageSize;
            } else {
                $customPageLimit = ($toolbarPageSize+2);
            }
        }
    // if page has aside only
    }elseif($toolbarAsideHtml) {
        if($current_page == 1) {
            if($toolbarUsesStoreSize == 't') {
                $customPageLimit = ($toolbarPageSize+2);
            } else {
                $customPageLimit = $toolbarPageSize;
            }
        } else {
            if($toolbarUsesStoreSize == 't') {
                $customPageLimit = $toolbarPageSize;
            } else {
                $customPageLimit = ($toolbarPageSize+2);
            }

        }
    // if page has featured item only
    }elseif($toolbarHasFeaturedItem){
        if($current_page == 1) {
            if($toolbarUsesStoreSize == 't') {
                $customPageLimit = ($toolbarPageSize+1);
            } else {
                $customPageLimit = ($toolbarPageSize+3);
            }
        }else{
            if($toolbarUsesStoreSize == 't') {
                if($toolbarOnPage == 't'){
                    $customPageLimit = ($toolbarPageSize+1);
                }else{
                    $customPageLimit = $toolbarPageSize;    
                }
            } else {
                $customPageLimit = ($toolbarPageSize+2);
            }
        }
    }

    $toolbar = $this->getToolbarBlock()->setData('_current_limit',$customPageLimit);
    //$toolbar = $this->getToolbarBlock();

    // called prepare sortable parameters
    $collection = $this->_getProductCollection();

    // use sortable parameters
    if ($orders = $this->getAvailableOrders()) {
        $toolbar->setAvailableOrders($orders);
    }
    if ($sort = $this->getSortBy()) {
        $toolbar->setDefaultOrder($sort);
    }
    if ($dir = $this->getDefaultDirection()) {
        $toolbar->setDefaultDirection($dir);
    }
    if ($modes = $this->getModes()) {
        $toolbar->setModes($modes);
    }

    // set collection to toolbar and apply sort
    $toolbar->setCollection($collection);

    $this->setChild('toolbar', $toolbar);
    Mage::dispatchEvent('catalog_block_product_list_collection', array(
        'collection' => $this->_getProductCollection()
    ));

    $this->_getProductCollection()->load();

    return parent::_beforeToHtml();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top