1

Edit: I think I found a solution. (See answer by me below)

I have a product collection which I pass to the Magento catalog/product_list block. I've set curPage and pageSize of the collection and pagination is working fine except the weird behavior I try to describe below.

In my case I have 15 products.

Default View Mode = Grid
Products per Page on Grid = 12 (Default Value)
Products per Page on List = 10 (Default Value)

When I view my collection in the catalog(frontend), default view mode is Grid, and I change the limit (Products per Page) to 24, all 15 products are shown on the first page. So far so good. But when I switch mode to list it shows me all 15 products on the first page but it also gives the possibility to go to the second page which contains the same 15 products.

Here my code:

    $curPage = 1;
    $mode = Mage::getStoreConfig('catalog/frontend/list_mode');
    $grid_products_per_page = explode(',', Mage::getStoreConfig('catalog/frontend/grid_per_page_values'));
    $list_products_per_page = explode(',', Mage::getStoreConfig('catalog/frontend/list_per_page_values'));

    //Default Values
    if($mode == 'grid' || $mode == 'grid-list') {
        $pageSize = Mage::getStoreConfig('catalog/frontend/grid_per_page');
    } else {
        $pageSize = Mage::getStoreConfig('catalog/frontend/list_per_page');
    }


    $mode = ($_GET['mode'] != null ? $_GET['mode'] : $mode);
    $pageSize = ($_GET['limit'] != null ? $_GET['limit'] : $pageSize);
    $curPage = ($_GET['p'] != null ? $_GET['p'] : $curPage);


    //Supposed to handle mode switch
    if($mode == 'grid') {
        $pageSize = $this->getClosestNumber($pageSize, $grid_products_per_page);
    } elseif($mode == 'list') {
        $pageSize = $this->getClosestNumber($pageSize, $list_products_per_page);        
    }

    /*
     * Fill product_result_ids array...
     */

    $productCollection = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToSelect('*')
        ->addIdFilter($product_result_ids);
    $productCollection->setPageSize($pageSize);
    $productCollection->setCurPage($curPage);
    $productCollection->count();   

    //The numbers seem to be correct
    echo $productCollection->count().'<br />';
    echo $curPage.'<br />';
    echo $pageSize.'<br />'; 

    return $productCollection;

If any more information is needed, please tell me. Thanks.

0

I think I figured it out. This is how it seems to work:

$curPage = 1;
$mode = Mage::getStoreConfig('catalog/frontend/list_mode');
$grid_products_per_page = explode(',', Mage::getStoreConfig('catalog/frontend/grid_per_page_values'));
$list_products_per_page = explode(',', Mage::getStoreConfig('catalog/frontend/list_per_page_values'));

//Default Values
if($mode == 'grid' || $mode == 'grid-list') {
    $pageSize = Mage::getStoreConfig('catalog/frontend/grid_per_page');
} else {
    $pageSize = Mage::getStoreConfig('catalog/frontend/list_per_page');
}


$mode = ($_GET['mode'] != null ? $_GET['mode'] : $mode);
$pageSize = ($_GET['limit'] != null ? $_GET['limit'] : $pageSize);
$curPage = ($_GET['p'] != null ? $_GET['p'] : $curPage);


//Supposed to handle mode switch (Changed part)
if($mode == 'grid' || $mode == 'grid-list') {
    if(in_array($pageSize, $grid_products_per_page) == false) {
        $pageSize = Mage::getStoreConfig('catalog/frontend/grid_per_page');
    }
} elseif($mode == 'list' || $mode == 'list-grid') {
    if(in_array($pageSize, $list_products_per_page) == false) {
        $pageSize = Mage::getStoreConfig('catalog/frontend/list_per_page');
    }
}

/*
 * Fill product_result_ids array...
 */

$productCollection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*')
    ->addIdFilter($product_result_ids);
$productCollection->setPageSize($pageSize);
$productCollection->setCurPage($curPage);
$productCollection->count();   

//The numbers seem to be correct
echo $productCollection->count().'<br />';
echo $curPage.'<br />';
echo $pageSize.'<br />'; 

return $productCollection;

To get this to work I had to observe the behavior of the toolbar a bit. If you are in catalog view and switch from grid mode to list mode (or other way around) the toolbar(not sure which class) checks if the $_GET['limit'] value is in the products_per_page array of the corresponding mode (grid or list). If not it picks the default value of this mode. I had to do it the same way to make sure the pageSize of my collection is synchronized with the toolbar.

if($mode == 'grid' || $mode == 'grid-list') {
    if(in_array($pageSize, $grid_products_per_page) == false) {
        $pageSize = Mage::getStoreConfig('catalog/frontend/grid_per_page');
    }
} elseif($mode == 'list' || $mode == 'list-grid') {
    if(in_array($pageSize, $list_products_per_page) == false) {
        $pageSize = Mage::getStoreConfig('catalog/frontend/list_per_page');
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.