Magento 1.9 Product Collection – Remove Attribute

attributesmagento-1.9productproduct-collection

I have created an observer that manipulates the advanced search result of Magento.
For this, I've used the controller_action_layout_render_before_catalogsearch_advanced_result observer.

I'm using an "dummy" attribute called "brand". This attribute doesn't accually contain something, but is just there so that the user can input a value in the normal magento forms.

Now my problem is that I can not seem to remove this dummy attribute. I've tried removeAttributeFromFilter() (and removeFieldFromFilter()) but it does not seem to disappear.

I cannot just reset the WHERE statement, because all other attributes do need to function like normal.

The observer:

class Jeroen_SearchFilter_Model_Observers_Searchresults
{
      function manipulateSQL( $event ){
         if(!Mage::app()->getRequest()->getOriginalPathInfo() == "/catalogsearch/advanced/result/";){ 
            //double check if this we're really using advanced search
            return;
         }

         $block = Mage::app()->getLayout()->getBlock('search_result_list');

         if($block){
             $collection = $block->getLoadedProductCollection();
             $searchFilterTable = Mage::getSingleton('core/resource')->getTableName('searchFilter/searchtable');
             $collection->getSelect()->join(array("searchfilter" => $searchFilterTable), "e.entity_id = searchfilter.product_id" );
             $collection->getSelect()->where("searchfilter.brandAttributeId = 'mybrand'");
             $collection->removeAttributeFromFilter("brand");
             $block->setLoadedProductCollection($collection);
         }
      }

}

Does anyone know how to remove the brand attribute from the collection?

Best Answer

No, you have to construct a new query. Once you hit the DB with a query with conditions, you can't just remove a condition and have new data, you have to construct a new query.

Afaik standard Magento does not have any methods to explicitely remove filters (besides clear(), which resets all filters and unloads the collection).

Check here for more information

Related Topic