cancel
Showing results for 
Search instead for 
Did you mean: 

Attempting to filter by first letter of product names but will only filter products on current page

Attempting to filter by first letter of product names but will only filter products on current page

My module is working on a fresh install of Magento CE 1.9 but not on my existing EE website. Here is the module on GIT. 

 

Using the configuration below, I am able to modify my collection, but it only applies to the results on the current pagination page. So, if I am on page one and I have a product starting with the letter A and modify the collection with ->addAttributeToFilter("name",["like"=>"a%"]) I see only the products that start with the letter a for the current page. If I am on page one and modify the collection with ->addAttributeToFilter("name",["like"=>"m%"]) I do not see products beginning with the letter M, I get Magento's "no products found" message. If I go to the pagination page that contains products with the letter M and use ->addAttributeToFilter("name",["like"=>"m%"]) I see only products starting with M that were on the current page.

How do I filter by letter prior to pagination and any other filters being applied?

config.xml

        <catalog_product_collection_apply_limitations_after>
            <observers>
                <etre_alphapagination>
                    <type>model</type>
                    <class>etre_alphapagination/observer</class>
                    <method>applyFilter</method>
                </etre_alphapagination>
            </observers>
        </catalog_product_collection_apply_limitations_after>

Observer.php

public function applyFilter($observer)
{

    if ($alphaFilter = Mage::getModel('etre_alphapagination/parametersactions')->currentAlphaFilter()) {
        $event = $observer->getEvent();
        $collection = Mage::getModel('etre_alphapagination/parametersactions')->applyFilter($event->getCollection(), $alphaFilter, "name");
        return $this;
    }
}

Mage::getModel('etre_alphapagination/parametersactions')->applyFilter($event->getCollection(), $alphaFilter, "name") refers to:

public function applyFilter($collection, $query = "", $attribute = 'name') //let's provide the option to filter the collection by whichever attribute desired
{
    try {
        $collection->addAttributeToFilter('name', array('like' => $query . '%'));
    } catch (Exception $e) {
        Mage::logException($e);
    }
    return $collection;
}

 

If I execute the code below, I get zero results on my filter.

public function getLoadedProductCollection()
{
    die(zend_debug::dump($this->_getProductCollection()->getData()));
    return $this->_getProductCollection();
}

 

If I execute the code bellow from anywhere within  $this->_getProductCollection() I see my expected results.

die(zend_debug::dump($this->_productCollection->getData()));