Hi, to be honest I haven't had the need to do this myself so can't offer any substantial code examples (there isn't an example in the git module examples repository either sadly).
The following in the ProductListingFrontController class appeared to be the key to the solution:
/** * This method is the heart of the search provider delegation * mechanism. * * It executes the `productSearchProvider` hook (array style), * and returns the first one encountered. * * This provides a well specified way for modules to execute * the search query instead of the core. * * The hook is called with the $query argument, which allows * modules to decide if they can manage the query. * * For instance, if two search modules are installed and * one module knows how to search by category but not by manufacturer, * then "ManufacturerController" will use one module to do the query while * "CategoryController" will use another module to do the query. * * If no module can perform the query then null is returned. * * @param ProductSearchQuery $query * * @return ProductSearchProviderInterface|null */ private function getProductSearchProviderFromModules($query) { // An array [module_name => module_output] will be returned $providers = Hook::exec( 'productSearchProvider', ['query' => $query], null, true ); if (!is_array($providers)) { $providers = []; } foreach ($providers as $provider) { if ($provider instanceof ProductSearchProviderInterface) { return $provider; } } return null; }
So your module will need to hook into productSearchProvider and respond if it can handle the query passed to it. It would appear that your module would return an array containing an object of class ProductSearchProviderInterface.
However, you should also see within this function in the ProductListingFrontController class (lines cut for readability):
/** * This returns all template variables needed for rendering * the product list, the facets, the pagination and the sort orders. * * @return array variables ready for templating */ protected function getProductSearchVariables() { .... // prepare the sort orders // note that, again, the product controller is sort-orders // agnostic // a module can easily add specific sort orders that it needs // to support (e.g. sort by "energy efficiency") $sort_orders = $this->getTemplateVarSortOrders( $result->getAvailableSortOrders(), $query->getSortOrder()->toString() ); .... $searchVariables = [ 'result' => $result, 'label' => $this->getListingLabel(), 'products' => $products, 'sort_orders' => $sort_orders, 'sort_selected' => $sort_selected, 'pagination' => $pagination, 'rendered_facets' => $rendered_facets, 'rendered_active_filters' => $rendered_active_filters, 'js_enabled' => $this->ajax, 'current_url' => $this->updateQueryString([ 'q' => $result->getEncodedFacets(), ]), ]; Hook::exec('filterProductSearch', ['searchVariables' => &$searchVariables]); Hook::exec('actionProductSearchAfter', $searchVariables); return $searchVariables; }
The second parameter to the getTemplateVarSortOrders leads to the function ProductSearchResult::getAvailableSortOrders(). This leads you back into the ps_faceted search module where you were attempting to inject your own sort order. A bit of a dead end - however as you can see there are two hooks at the end of this function that would allow you the opportunity of modifying the sort orders, so I assume you could implement the hook filterProductSearch in your module. I'm guessing you need to insert your own sort order based on the content of the getTemplateVarSortOrders function called above, i.e.
/** * Prepares the sort-order links. * * Sort order links contain the current encoded facets if any, * but not the page number because normally when you change the sort order * you want to go back to page one. * * @param array $sortOrders the available sort orders * @param string $currentSortOrderURLParameter used to know which of the sort orders (if any) is active * * @return array */ protected function getTemplateVarSortOrders(array $sortOrders, $currentSortOrderURLParameter) { return array_map(function ($sortOrder) use ($currentSortOrderURLParameter) { $order = $sortOrder->toArray(); $order['current'] = $order['urlParameter'] === $currentSortOrderURLParameter; $order['url'] = $this->updateQueryString([ 'order' => $order['urlParameter'], 'page' => null, ]); return $order; }, $sortOrders); }
It appears that this sets the parameters for the SQL that sorts the query, and you may just have to focus on this hook and perhaps don't need to worry about the full-blown ProductSearchProviderInterface route.....
Not sure if any of this rambling will help you though! It's a shame that very little of this is documented but it's getting there slowly.
EDIT: I just realised that I only discussed the function ProductSearchResult::getAvailableSortOrders() in passing, but the contents of this will give vital clues for the correct form of the custom sort order you're looking to create.