Allows third-party modules to register custom product search extension functions.
The event handler should accept two parameters - the $options array passed to the
Shop_Product::find_products()
method the search query string. The handler should return a name of a search event handled in the custom module. Example:
public function subscribeEvents()
{
Backend::$events->addEvent('shop:onRegisterProductSearchEvent', $this, 'register_search_event');
Backend::$events->addEvent('colorsearch:findExtraOptions', $this, 'find_extra_options');
}
public function register_search_event($options, $query)
{
return "colorsearch:findExtraOptions";
}
The event which name is returned by the handler should be handled in the same module. This handler should accept
three parameters - the search options array, the search query template string and the search query string.
The handler should extend the search query template string and return the updated template. The following method
finds all products which have an extra option named "Red" in the extra option group "Color". For simplicity we
hardcoded the option and group name, but in real conditions you would pass these values via the search options array.
public function find_extra_options($options, $template, $query)
{
$extra_group = 'Color';
$value = 'Red';
$extra_group = mysql_real_escape_string($extra_group);
$value = mysql_real_escape_string($value);
$group_query = "(
exists(
select
id
from
shop_extra_options
where
group_name='".$extra_group."'
and shop_extra_options.description like '%".$value."%'
and product_id=shop_products.id and option_in_set is null
)
or
exists(
select
shop_extra_options.id
from
shop_extra_options,
shop_extra_option_sets,
shop_products_extra_sets
where
shop_products_extra_sets.extra_product_id = shop_products.id
and shop_products_extra_sets.extra_option_set_id = shop_extra_option_sets.id
and shop_extra_options.option_in_set = 1
and shop_extra_options.product_id = shop_extra_option_sets.id
and group_name='".$extra_group."' and shop_extra_options.description like '%".$value."%'
)
)";
return Shop_Product::set_search_query_params($template, '%TABLES%', $group_query.' and %FILTER%');
}
Note that the code uses the Shop_Product::set_search_query_params() method. This method formats the product search query
template by adding new items to the table list and to the filters clause.
This event affects the
Search Page and
Shop_Product::find_products() method.