Last active
June 7, 2023 05:12
-
-
Save thaitranet/93b6ef8b7c8de55f5d974dbb28b14dff to your computer and use it in GitHub Desktop.
Enhance Your Sitecore Experience with SXA Search and Discover Integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
namespace Platform.Models | |
{ | |
public class ContentParameters | |
{ | |
public ProductParameters product { get; set; } | |
} | |
public class ContextParameters | |
{ | |
public PageParameters page { get; set; } | |
} | |
public class Data | |
{ | |
public WidgetParameters widget { get; set; } | |
public ContextParameters context { get; set; } | |
public QueryParameters query { get; set; } | |
public ContentParameters content { get; set; } | |
} | |
public class Keyphrase | |
{ | |
public List<string> value { get; set; } | |
} | |
public class PageParameters | |
{ | |
public string locale_country { get; set; } | |
public string locale_language { get; set; } | |
} | |
public class ProductParameters | |
{ | |
} | |
public class QueryParameters | |
{ | |
public Keyphrase keyphrase { get; set; } | |
} | |
public class DiscoverPreviewSearchRequest | |
{ | |
public Data data { get; set; } | |
} | |
public class WidgetParameters | |
{ | |
public string rfkid { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
namespace Platform.Models | |
{ | |
public class Content | |
{ | |
public Product product { get; set; } | |
} | |
public class CustomSpecificationsAu | |
{ | |
public string class_id { get; set; } | |
public string class_friendly_name { get; set; } | |
public string emc_version { get; set; } | |
public string colour { get; set; } | |
public string material { get; set; } | |
public string with_backside_door { get; set; } | |
public string with_ventilation_door { get; set; } | |
public string suitable_for_outdoor_set_up { get; set; } | |
public string impact_strength { get; set; } | |
public string with_glazed_door { get; set; } | |
public string number_of_locks { get; set; } | |
public string number_of_doors { get; set; } | |
public string pitched_roof { get; set; } | |
public string with_mounting_plate { get; set; } | |
public string pole_fastening { get; set; } | |
public string suitable_for_wall_mounting { get; set; } | |
public string mounting_plate_depth_adjustable { get; set; } | |
public string height { get; set; } | |
public string depth { get; set; } | |
public string width { get; set; } | |
public string degree_of_protection_ip { get; set; } | |
} | |
public class CustomSpecificationsNz | |
{ | |
public string class_id { get; set; } | |
public string class_friendly_name { get; set; } | |
public string emc_version { get; set; } | |
public string colour { get; set; } | |
public string material { get; set; } | |
public string with_backside_door { get; set; } | |
public string with_ventilation_door { get; set; } | |
public string suitable_for_outdoor_set_up { get; set; } | |
public string impact_strength { get; set; } | |
public string with_glazed_door { get; set; } | |
public string number_of_locks { get; set; } | |
public string number_of_doors { get; set; } | |
public string pitched_roof { get; set; } | |
public string with_mounting_plate { get; set; } | |
public string pole_fastening { get; set; } | |
public string suitable_for_wall_mounting { get; set; } | |
public string mounting_plate_depth_adjustable { get; set; } | |
public string height { get; set; } | |
public string depth { get; set; } | |
public string width { get; set; } | |
public string degree_of_protection_ip { get; set; } | |
} | |
public class Product | |
{ | |
public int total_item { get; set; } | |
public List<Value> value { get; set; } | |
public int n_item { get; set; } | |
} | |
public class Query2id | |
{ | |
public string keyphrase { get; set; } | |
} | |
public class DiscoverPreviewSearchResponse | |
{ | |
public Query2id query2id { get; set; } | |
public Content content { get; set; } | |
public int n_item { get; set; } | |
public int total_item { get; set; } | |
public int page_number { get; set; } | |
public int total_page { get; set; } | |
public Widget widget { get; set; } | |
public string rid { get; set; } | |
public string url { get; set; } | |
public long ts { get; set; } | |
public int dt { get; set; } | |
} | |
public class Value | |
{ | |
public string custom_brand { get; set; } | |
public string brand_code { get; set; } | |
public string image_url { get; set; } | |
public List<string> all_category_ids { get; set; } | |
public List<string> category_names { get; set; } | |
public string custom_is_group_600 { get; set; } | |
public string product_type { get; set; } | |
public string release_date { get; set; } | |
public string available_nz { get; set; } | |
public List<string> all_category_names { get; set; } | |
public string custom_item_group { get; set; } | |
public string name { get; set; } | |
public int id { get; set; } | |
public List<string> category_ids { get; set; } | |
public string product_group { get; set; } | |
public string available_au { get; set; } | |
public string stock_unit { get; set; } | |
public string breadcrumbs { get; set; } | |
public string product_url { get; set; } | |
public string custom_long_description { get; set; } | |
public string custom_etim_class { get; set; } | |
public string custom_web_product_status { get; set; } | |
public string custom_company { get; set; } | |
public string description { get; set; } | |
public string custom_product_range { get; set; } | |
public string custom_etim_class_id { get; set; } | |
public string sku { get; set; } | |
public int skuid { get; set; } | |
public string price { get; set; } | |
public string final_price { get; set; } | |
public CustomSpecificationsNz custom_specifications_nz { get; set; } | |
public CustomSpecificationsAu custom_specifications_au { get; set; } | |
} | |
public class Widget | |
{ | |
public string rfkid { get; set; } | |
public string used_in { get; set; } | |
public string variation_id { get; set; } | |
public string type { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Sitecore.XA.Feature.Search.Models; | |
namespace Platform.Models | |
{ | |
public class DiscoverPreviewSearchResult : Result | |
{ | |
public string Image { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
using Platform.Models; | |
using Sitecore.XA.Feature.Search.Attributes; | |
using Sitecore.XA.Feature.Search.Binder; | |
using Sitecore.XA.Feature.Search.Controllers; | |
using Sitecore.XA.Feature.Search.Filters; | |
using Sitecore.XA.Feature.Search.Models; | |
using Sitecore.XA.Foundation.Search.Models.Binding; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using System.Web.Http.ModelBinding; | |
namespace Platform.Controllers | |
{ | |
[JsonFormatter] | |
public class XASearchOverrideController : SearchController | |
{ | |
private readonly object DISCOVER_DOMAIN_ID = ""; | |
private readonly string DISCOVER_AUTHORIZATION_KEY = ""; | |
[RegisterSearchEvent] | |
[CacheWebApi] | |
public async Task<ResultSet> GetResultsOverride([ModelBinder(BinderType = typeof(QueryModelBinder))] QueryModel model) | |
{ | |
var searchResults = GetResults(model); | |
var discoverSearchResults = await PerformDiscoverPreviewSearch(model); | |
if (discoverSearchResults != null) | |
{ | |
searchResults.Results = discoverSearchResults.Concat(searchResults.Results); | |
} | |
return searchResults; | |
} | |
public async Task<IEnumerable<DiscoverPreviewSearchResult>> PerformDiscoverPreviewSearch(QueryModel model) | |
{ | |
var client = new HttpClient(); | |
var request = new HttpRequestMessage(HttpMethod.Post, $"https://api-apse2.rfksrv.com/search-rec/{DISCOVER_DOMAIN_ID}/3"); | |
request.Headers.Add("authorization", DISCOVER_AUTHORIZATION_KEY); | |
var parameters = new DiscoverPreviewSearchRequest | |
{ | |
data = new Data | |
{ | |
widget = new WidgetParameters | |
{ | |
rfkid = "rfkid_6" | |
}, | |
context = new ContextParameters | |
{ | |
page = new PageParameters | |
{ | |
locale_country = "au", | |
locale_language = "en" | |
} | |
}, | |
query = new QueryParameters | |
{ | |
keyphrase = new Keyphrase | |
{ | |
value = new List<string> { model.Query } | |
} | |
}, | |
content = new ContentParameters | |
{ | |
product = new ProductParameters { } | |
} | |
} | |
}; | |
var content = new StringContent(JsonConvert.SerializeObject(parameters), null, "application/json"); | |
request.Content = content; | |
var response = await client.SendAsync(request); | |
response.EnsureSuccessStatusCode(); | |
var jsonResponse = await response.Content.ReadAsStringAsync(); | |
var result = JsonConvert.DeserializeObject<DiscoverPreviewSearchResponse>(jsonResponse); | |
if (result?.content?.product?.value != null) | |
{ | |
return result.content.product.value.Select(delegate (Value item) | |
{ | |
return new DiscoverPreviewSearchResult | |
{ | |
Name = item.name, | |
Url = item.product_url, | |
Image = item.image_url, | |
Html = $"\r\n<div class=\"product-summary\" data-id=\"7042142\">\r\n \r\n \r\n <div class=\"product-photo\">\r\n \r\n <a title=\"Optix USB Smart Card Reader\" href=\"/Catalogs/Habitat_Master/Habitat_Master-Departments/Habitat_Master-Cameras/7042142\">\r\n <img src=\"https://placehold.co/220\" alt=\"\" />\r\n \r\n <div data-bind=\"css: {{ 'promotion': promotion }}\"\r\n class=\"hidden \">\r\n \r\n <span data-bind=\"text: promotion\" class=\"savings-percentage\">0%</span>\r\n <span class=\"savings-label\">off</span>\r\n </div>\r\n </a>\r\n </div>\r\n \r\n <div class=\"product-info\">\r\n \r\n <a class=\"product-title\" title=\"Optix USB Smart Card Reader\" href=\"/Catalogs/Habitat_Master/Habitat_Master-Departments/Habitat_Master-Cameras/7042142\">\r\n {item.id}\r\n </a>\r\n <div class=\"product-brand\">\r\n Sitecore Discover\r\n </div>\r\n \r\n <div class=\"product-indicator\">\r\n \r\n <label class=\"hidden \"\r\n data-bind=\"css: {{ 'price-difference': hasDifferentPrice }}\">\r\n Starting from\r\n </label>\r\n <label class=\"stock-status\" data-bind=\"text: stockLabel\">In-Stock</label>\r\n </div>\r\n \r\n <div class=\"product-detail \"\r\n data-bind=\"css: {{ 'on-sale': promotion }}\">\r\n \r\n <span data-bind=\"text: price\" class=\"product-price\">30.79 USD</span>\r\n <a class=\"product-link\" title=\"Optix USB Smart Card Reader\" href=\"/Catalogs/Habitat_Master/Habitat_Master-Departments/Habitat_Master-Cameras/7042142\">\r\n Details\r\n </a>\r\n </div>\r\n </div>\r\n</div>\r\n" | |
}; | |
}); | |
} | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment