2

I want to load the search results by user’s current location by default or first time when user lands on the page.

Does Sitecore supports this feature OOTB or or any custom solution available?

I’m thinking of custom filter with no UI just JavaScript part, where Filter will be responsible for getting the user location and update the hash of the URL with coordinates.

Do you see any issue with this approach?

Please provide your suggestions or idea.

SXA 1.9

2 Answers 2

2

I'm not aware of an ootb solution. What you could do is create a custom search token that handles the logic you need. You can add that token to the search scope that you use for the search components.

More info: https://ggullentops.blogspot.com/2019/01/extending-sxa-search-query-tokens.html

You need to create a ResolveSearchQueryTokensProcessor. Override the public override void Process(ResolveSearchQueryTokensEventArgs args) method:

First you need to search your token (loop over all SearchStringModels in args.Model) Each SearchStringModel defines a part of the query and has a value, a type and an operation:

  • the type of the SearchStringModel is the first part
  • the value of the model is the second part (e.g. "ItemsWithTheSameValueInField|FieldName")
  • the operation is "must", "should" or "not"

Once you found the model, you need to create a new one that can be send to the index and remove the one you handled. Building the search model is something like:

protected virtual SearchStringModel BuildModel(string fieldName, string fieldValue)
{
  var name = SomeBusinessLogicWithFieldName(fieldName);
  var value = SomeBusinessLogicWithFieldValue(fieldValue);
  return new SearchStringModel("custom", FormattableString.Invariant(FormattableStringFactory.Create("{0}|{1}", name, value)))
  {
    Operation = "must"
  };
}

Add your processor the the <resolveSearchQueryTokens> pipeline and it should appear as option in the search tokens so you can add it to the scope.

In your case you'll need to add the location logic (or any other logic you want) to the BuildModel. That function is custom, so you can also arrange the parameters as you want. You just need to be able to pass everything from the Process method.

6
  • Does Sitecore’s Geo Ip service provides the IP details of the user? Commented Aug 14, 2019 at 8:53
  • The IP address is in the Request. Sitecore Geo IP service will provide extra information based on that IP.
    – Gatogordo
    Commented Aug 14, 2019 at 8:55
  • Sorry I mean Geo location:) Commented Aug 14, 2019 at 9:38
  • Yes, you should be able to get that with the geo ip service
    – Gatogordo
    Commented Aug 14, 2019 at 10:48
  • Thanks @Gatogordo Commented Aug 14, 2019 at 10:50
1

You can use the existing JS API. Take a look at how it is done in the Location Finder script (component-search-location-filter.js). Base on the mode it can translate the location name provided by the user or just detect current location. To detect the current location it is using HTML Geolocation API present in the browser (the code for it is located in component-location-service.js).

This is how you can get current user location for the user inside of your filter script:

XA.component.locationService.detectLocation(
     function (location) {
          ...
     },
     function (errorMessage) {
          console.log(errorMessage);
     }
);
5
  • I believe you mean create a new filter which uses search API like you mentioned if I get the location then update the hash with “g” parameter. And my search results should respect the parameter. Correct me if my understanding is not correct. Also we are not using location finder as client has local postal JS as data source. Commented Aug 15, 2019 at 8:45
  • You wrote that "I’m thinking of custom filter with no UI just JavaScript part". You can do that and the script will be quite simple (actually it event doesn't need to be a filter rendering, you can include this script in your theme) as I have shown above. Commented Aug 15, 2019 at 8:48
  • Thanks for the clarification. Commented Aug 15, 2019 at 8:52
  • Quick question then that means if any random script updates the hash parameter will affect the search results? Commented Aug 15, 2019 at 8:54
  • Yes. Any hash changes will cause Search Results to reload. Commented Aug 15, 2019 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.