0

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?

1
+50

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

| improve this answer | |
  • This is what I have now, plus array and range support (from, to price for example). – Jeroen Mar 29 '16 at 7:52
0

To my knowledge the only way is to edit the where part and remove the part where the condition is set on the attribute you want to remove:

$where = $collection->getSelect()->getPart(Zend_Db_Select::WHERE);
foreach ($where as $key => $condition)
{
    // You may have to adapt this condition depending on the situation
    if (strpos("brand",$condition) !== false)
    {
        unset($where[$key]);
    }
}
$collection->getSelect()->setPart(Zend_Db_Select::WHERE, $where);
| improve this answer | |

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.