Magento Autocomplete js Source Options

I have a client that needs a specific set up with regards the search. They have customers that have a restricted catalog and so certain results should not be included in the response on a per customer basis.

To test how possible this was to do I modified the common.js file in the plugin and added options.facetFilters = 'sku:-30066';. This worked perfectly but is clearly not the correct way to do it as I don’t want to modify any core files. Also I have had to make sku a facet which is not ideal but okay I think.

What is the best approach for this?

I have experimented with adding a custom source to the autocomplete.js that is basically a copy of the products source in the common.js with some minor tweaks.

Ideally at run time I would just like to set an exclude list and the plugin reads that in as default options.

I am using v1.9.0 of the plugin

Hello @asmart,

the best way to exclude some records for any group of customers is to use Secured API key. In this kind of key you can specify any filter you want.

You can specify the key by hooking your code to algolia_after_create_configuration custom event. In the hook you’ll be provided with complete configuration array where you can replace the API key with your secured one.
For inspiration you can take a look how the key is generated regularly in Magento: https://github.com/algolia/algoliasearch-magento/blob/master/app/design/frontend/base/default/template/algoliasearch/internals/configuration.phtml#L182-L191

If you don’t want to filter based on SKU attribute, I’d recommend you to create your custom attribute which will define which customers can see the product. That you can do in algolia_after_create_product_object custom event. To this even you’ll get the array which will be indexed in Algolia and you can enrich it with your custom attribute.

Finally you’ll need to set this attribute as attribute for faceting. As you most probably won’t have this attribute in Magento’s backend, you can again hook your code to algolia_products_index_before_set_settings event where you are able to modify the settings sent to Algolia.

That’s it :slight_smile:

If you need help with hooking the code to the events, you can follow this guide: https://community.algolia.com/magento/doc/m1/customize-backend/

Let me know how it goes or if you need any other help.

Hey.

Thanks :slight_smile:

I have been playing around with it and can only get it to include certain results rather than exclude.

So, ['filters' => 'sku:30066'] works when added to the secure key and only produces that result in the autocomplete. If i negate the sku -30066 I then to do not get any results for any search term whereas I would expect it to just exclude that one sku from the autocomplete.

Thanks

With filters the - sign is not supported.
To make it work you need to use the keyword NOT.

So in you case it’d look like: ['filters' => 'NOT sku:30066'].

Really appreciate you looking at this.

I have changed it to “NOT” and this is the now the query;
query=meat&hitsPerPage=6&analyticsTags=autocomplete&facets=%5B%22categories.level0%22%5D&numericFilters=visibility_search%3D1&filters=NOT+sku%3A30066

Product with sku 30066 is still being included in the autocomplete though :frowning: