2

For a specific store i want an remove search results, if search query matches our SKU pattern.

  1. disable "searchable" in attribute config is no solution
  2. rewrite core files is not wanted

So, what i need is an event which let me manipulate the search query or product collection for search result page.

I tried following events.

  1. catalogsearch_searchable_attributes_load_after ... not triggered
  2. catalogsearch_query(_load_before/after) ... also not triggered
  3. and some others ... ;)

... but without success.

Working solutions so far ... using catalog_product_collection_load_before with following observer:

public function test(Varien_Event_Observer $observer)
{
    // some logic for check current store
    // ...

    $action = Mage::app()->getFrontController()->getAction()->getFullActionName();
    if ($action === 'catalogsearch_result_index') {
        $query = Mage::app()->getRequest()->getParam('q');
        if (preg_match("/my regex/", $query)) {
            // force empty collection with invalid entity_id
            $observer->getCollection()->addFieldToFilter('entity_id', false);
        }
    }
}

.but this looks like an ugly hack to me and shouldn't be final.

Hope you have some better approaches - maybe an more suitable event, that's no triggered for every product collection?

Update 1:

Regex check isn't really good, because partial search still works.

Preferred solution would be to disable SKU from searchable fields without disbale it globally from attribute configuration.

Update 2:

Instead of using regex i switched over to this, because it seems more correct if sku pattern is part of name/description/... and sould be searchable:

$observer->getCollection()->addFieldToFilter('sku', array('nlike' => "%{$query}%"));

1 Answer 1

0

Since there is no event like catalogsearch_result_product_collection_load_before my solution affects every product collection on searchresult page (bestsellers in sidebar, new products ...).

Thanks to Marius I found a good solution.

I changed event to controller_action_layout_render_before_catalogsearch_result_index

and Observer.php to:

const RESULT_BLOCK_NAME = 'search_result_list';
public function test($observer)
{
    if (Mage::getStoreConfigFlag('catalog/frontend/disable_sku_search')) {
        /** @var Mage_Catalog_Block_Product_List $block */
        $block = Mage::app()->getLayout()->getBlock(self::RESULT_BLOCK_NAME);
        if ($block) {
            if ($collection = $block->getLoadedProductCollection()) {
                $query = Mage::app()->getRequest()->getParam('q');
                $collection->addFieldToFilter('sku', array('nlike' => "%{$query}%"));
            }
        }
    }
}

Download: https://github.com/sreichel/magento-StackExchange_DisableSearchBySku

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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