Magento Forum

   
Forcing repagination after filtering a collection
 
hmpierson
Sr. Member
 
Total Posts:  101
Joined:  2008-11-15
 

Hi,

I’m trying to add an additional “toolbar” line that lets me filter within a collection by “First Letter” of the product name.

I’m most of the way there, my problem is that after I correctly filter, the resulting pagination is the same as before the addtional filtering.

getSize reports the same collection size after the filtering as before, although iterating through the collection produces the correct results (ie limited to the only the products with the requested first letter.)

I’ve been pulling my hair out trying to force the repagination. I suppose if I get desperate enough, I can replace the pagination directly in list.phtml if I find the “let” variable in the query string (ie do my own calculations for first and last, links for pagination etc) but that’s a lot of unnecessary work if I can get the toolbar to recognize the refined collection.

here’s the code from list.phtml (I’m still using MAgento 1.4.0.1 on this site, but the code is almost identical (one additional line) in my Magento 1.7.0.0 website.

Below the code are two images the first with no filtering by letter on the same category, the second a “refined” page for products only beginning with the letter T, . FYI, I’m also using the most excellent Improved Navigation extension.

<?php $_productCollection$this->getLoadedProductCollection();

//additional code to set up the HTML for refined navigation by First Letter of product name
$_pagesize $_productCollection->getPageSize();
$letarray = array('#','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
$let = array();
foreach (
$letarray as $l{$let[$l] $l;}

$currentUrl 
'http://' $_SERVER["SERVER_NAME"$_SERVER['REQUEST_URI'];
$no_query explode('?'$currentUrl);
$init_url $no_query[0] '?';

$collX = clone  $_productCollection;//create a clone of the products
$collX->clear();//this shold mark the collection as not loaded and execute the sql again on the next iteration.
$collX->setPage(1999999);//this sets the limit to the select. page 1 and number if items 999999 - or any other large number.

foreach ($collX as $cprod{
$_name 
strtoupper($cprod->getName());
if (
$_name[0] 'A'{if ($let['#'== '#'{$let['#'"<a href=\"$init_url" . 'let=1">#</a>';}} else {
if ($_name[0] == $let[$_name[0]])  {$let[$_name[0]] "<a href=\"" . $init_url . 'let=' . $_name[0] . '">' . $_name[0] . '</a>';}}    
}

$sto = "<ol><li><a href=\"" . $no_query[0] . "\">ALL</a></li><li>|</li>\n";
foreach ($letarray as $l) {$sto .= "<li>" . $let[$l] . "</li>\n";}
$sto .= "</ol>\n";
//end of additional code to set up HTML for refined navigation by First Letter of product name
?>

<?php if(!$_productCollection->count()): ?>
<p class="note-msg"><?php echo $this->__('
There are no products matching the selection.') ?></p>
<?php else: ?>
<div class="category-products">
    <!-- next line is the HTML for the additional navigation bar, developed entirely by the above additional code -->
    <div class="pager" style="color : LightGray;"><div style="float:left; color:black;">Filter Products by Letter</div><div class="pages"><?php echo $sto; ?></div></div>

    <!-- now do the additional filtering if the request variable '
let' is in the query string -->
    
    <?php if (isset($_REQUEST['
let']) && (in_array($_REQUEST['let'], $letarray) || $_REQUEST['let'] == '1'))  : ?>
    <?php
    $_productCollection->clear();
    if ($_REQUEST['
let'] == '1') {
     $_productCollection->addAttributeToFilter('
name',array('gteq' => '0'));
     $_productCollection->addAttributeToFilter('
name',array('lteq' => '9z'));   
    }
    else {
    $_productCollection->addAttributeToFilter('
name',array('like' => $_REQUEST['let'] . '%'));
        }

    //end of additional fitering code if filtering for products beginning with a request letter, or filtered on begining with a numberal    

    ?>
    <?php endif ?>
    <?php echo $this->getToolbarHtml() ?>

Image Attachments
repage2.PNGrepage1.PNG
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top