IMPORTANT : The methods within this article are being replaced!
Please review the following updated guides for efficient customization:
To modify a search, the outgoing query to the indexed catalog is intercepted and any of the available Klevu API parameters are available to be inserted, or adjusted as needed.
Reference the API to understand what parameters are available to meet the objective
Attach the override to the appropriate outgoing query (or queries)
Please see Custom Overrides for the prerequisite details on preparing for Klevu JS overriding.
Any of the Klevu API paramters are available to be applied as needed or otherwise to override any default values.
For example, the following example snippet modifies the outgoing productList API Query by assigning the value of "20" as the number of filter options to return.
klevu.setObjectPath( data , "localOverrides.query.productList.filters.filtersToReturn.options.limit", 20)
The object path notation corresponds to that used in the outgoing JSON API query
"recordQueries": [
{
"filters": {
"filtersToReturn": {
"options": {
"limit": 20
}
}
},
"id": "productList",
"typeOfRequest": "SEARCH",
"settings": {
"query": {
"term": "jackets"
}
}
}
],
Please see Custom Overrides for details on preparing for Klevu query overriding.
The query parameters are accessible by attaching to the query request.control within a previously established custom event listener.
klevu.coreEvent.attach("myLandingPageOverride", {
name: "attachToMyLandingPageOverride",
fire: function () {
// Attach to the Klevu query request chain
klevu.search.landing.getScope().chains.request.control.addAfter(
"initRequest", {
name: "modifyLandingQuery",
fire: function (data, scope) {
// OVERRIDE API PARAMETER HERE
}
});
klevu({
powerUp: {
landing: true
}
});
}
});
Now the query override can be applied
klevu.coreEvent.attach("myLandingPageOverride", {
name: "attachToMyLandingPageOverride",
fire: function () {
// Attach to the Klevu query request chain
klevu.search.landing.getScope().chains.request.control.addAfter(
"initRequest", {
name: "modifyLandingQuery",
fire: function (data, scope) {
// OVERRIDE API PARAMETER HERE
// SET max filter options to retrieve
klevu.setObjectPath( data , "localOverrides.query.productList.filters.filtersToReturn.options.limit", 20);
}
});
klevu({
powerUp: {
landing: true
}
});
}
});
Any of the Klevu API parameters are available to be applied as needed or otherwise override any default values.
Landing page search query override to change the max filter options to retrieve for display:
<script type="text/javascript">
klevu({
powerUp: {
landing: false
}
});
klevu.coreEvent.build({
name: "myLandingPageOverride",
fire: function () {
if (klevu.getGlobalSetting("flags.setRemoteConfigLanding.build", false)) {
return true;
}
return false;
},
maxCount: 150,
delay: 100
});
klevu.coreEvent.attach("myLandingPageOverride", {
name: "attachToMyLandingPageOverride",
fire: function () {
klevu.search.landing.getScope().chains.request.control.addAfter("initRequest", {
name: "modifyLandingQuery",
fire: function (data, scope) {
klevu.setObjectPath( data ,
"localOverrides.query.productList.filters.filtersToReturn.options.limit",
20);
}
});
klevu({
powerUp: {
landing: true
}
});
}
});
</script>
Note that the action in the above example requires definition within the corresponding scope of the override.
See an example below for establishing a common function that can be used to apply shared query modifications across multiple scope.
Often query modifications are intended to be applied to any search query. For this reason a common function can be created in which to apply the override. This is a useful strategy to apply override parameters to every query across quick, landing, and catnav scope.
Please see Custom Overrides for details on preparing for Klevu query overriding.
The query parameters are accessible by attaching to the query request.control within a previously established custom event listener.
In this example we are passing the arguments to a common function object reference as myCommonQueryOverride
klevu.coreEvent.attach("myLandingPageOverride", {
name: "attachToMyLandingPageOverride",
fire: function () {
// Attach to the Klevu query request chain
klevu.search.landing.getScope().chains.request.control.addAfter(
"initRequest", {
name: "modifyLandingQuery",
fire: function (data, scope) {
// REFERENCE COMMON FUNCTION HERE
klevu.search.modules.myCommonQueryOverride(data, scope);
}
});
klevu({
powerUp: {
landing: true
}
});
}
});
Now, we will define the new common function myCommonQueryOverride that is called from the custom event listener in the previous step.
This function will loop through all outgoing queries and apply the override.
Note the use of a dynamic query.id used in the loop
(function (klevu) {
klevu.extend(true, klevu.search.modules, {
myCommonQueryOverride: function (data, scope) {
klevu.each(data.request.current.recordQueries,function(key, query){
// OVERRIDE API PARAMETER HERE
// SET active currency
klevu.setObjectPath(
data,
"localOverrides.query." + query.id + ".settings.priceFieldSuffix",
"GBP"
);
});
}
});
})(klevu);
Any of the Klevu API parameters are available to be applied as needed or otherwise override any default values.
Landing page and Quick search query common function override to change the Currency used for pricing.
<script type="text/javascript">
klevu({
powerUp: {
quick: false,
landing: false,
catnav: false
}
});
klevu.coreEvent.build({
name: "myQuickSearchOverride",
fire: function () {
if (klevu.getGlobalSetting("flags.setRemoteConfigQuick.build", false)) {
return true;
}
return false;
},
maxCount: 150,
delay: 100
});
klevu.coreEvent.attach("myQuickSearchOverride", {
name: "attachToMyQuickSearchOverridee",
fire: function () {
klevu.search.quick.getScope().chains.request.control.addAfter("initRequest", {
name: "modifyQuickQuery",
fire: function (data, scope) {
klevu.search.modules.addMyOverrideToQuery(data, scope);
}
});
klevu({
powerUp: {
quick: true
}
});
}
});
klevu.coreEvent.build({
name: "myLandingPageOverride",
fire: function () {
if (klevu.getGlobalSetting("flags.setRemoteConfigLanding.build", false)) {
return true;
}
return false;
},
maxCount: 150,
delay: 100
});
klevu.coreEvent.attach("myLandingPageOverride", {
name: "attachToMyLandingPageOverride",
fire: function () {
klevu.search.landing.getScope().chains.request.control.addAfter("initRequest", {
name: "modifyLandingQuery",
fire: function (data, scope) {
klevu.search.modules.addMyOverrideToQuery(data, scope);
}
});
klevu({
powerUp: {
landing: true
}
});
}
});
klevu.coreEvent.build({
name: "myCategoryPageOverride",
fire: function () {
if (klevu.getGlobalSetting("flags.setRemoteConfigCatnav.build", false)) {
return true;
}
return false;
},
maxCount: 150,
delay: 100
});
klevu.coreEvent.attach("myCategoryPageOverride", {
name: "attachToMyCategoryPageOverride",
fire: function () {
klevu.search.catnav.getScope().chains.request.control.addAfter("initRequest", {
name: "modifyCatnavQuery",
fire: function (data, scope) {
klevu.search.modules.addMyOverrideToQuery(data, scope);
}
});
klevu({
powerUp: {
catnav: true
}
});
}
});
(function (klevu) {
klevu.extend(true, klevu.search.modules, {
addMyOverrideToQuery: function (data, scope) {
klevu.each(data.request.current.recordQueries,function(key, query){
klevu.setObjectPath(
data,
"localOverrides.query." + query.id + ".settings.priceFieldSuffix",
"GBP"
);
});
}
});
})(klevu);
</script>