LemonStand Documentation

shop:onRegisterProductSearchEvent event

The shop:onRegisterProductSearchEvent event allows third-party modules to register custom product search extension functions. The event handler should accept a single parameter - the $options array passed to the find_products() method of the Shop_Product class. The event handler function 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()
{
  return "colorsearch:findExtraOptions";
}

The event which name is returned by the shop:onRegisterProductSearchEvent handler should be handled in the same module. This handler should accept two parameters - the search options array, and the search query template string. The method should extend the search query template string and return the update string. The following method find 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)
{
  $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%');
}

Please 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. 

Now you can use the standard search page, or implement a custom search page using the find_products() method of the Shop_Product class:

$pagination = new Phpr_Pagination(10);
$products = Shop_Product::find_products(null, $pagination, 1, array('some_option'=>'some_value'));

Next: core:onInitialize event
Previous: shop:onCustomerUpdated event
Return to Handling LemonStand events