Searching for SKU without special characters
To make this work for both, the real SKU and the one without -
, both have to be added to catalogsearch_result
table because SQL-query for search results looks like this:
SELECT `s`.`product_id`, 0 AS `relevance`
FROM `catalogsearch_fulltext` AS `s`
INNER JOIN `catalog_product_entity` AS `e` ON e.entity_id = s.product_id
WHERE (s.store_id = 1) AND ((`s`.`data_index` LIKE '%sku-123%'))
I will not give an answer where you have to override core files and I'm still playing arround with catalog search reindexing process to find a way to make it work event-observer
based.
As (temporarily?) solution that ends up in the same result as modifiying index process, you can do this:
- create a new searchable product attribute from backend, let's call it
sku_search
- add an observer that listens to
catalog_product_save_before
to "autofill" this attribute - Reindex
Catalog Search Index
- DONE.
Observer code could look like this:
class My_Module_Model_Observer extends Mage_Core_Model_Observer
{
public function setSkuSearchAttribute($observer)
{
$product = $observer->getProduct();
$product->setSkuSearch(str_replace('-', '', $product->geSku()));
}
}
I would add something, to prevent that this attribute is manually edited from backend, like another observer for catalog_product_edit_action
with this code:
class My_Module_Model_Observer extends Mage_Core_Model_Observer
{
public function lockProductAttribute(Varien_Event_Observer $observer)
{
$product = $observer->getProduct();
$product->lockAttribute('sku_search');
}
}