Using feed or Backend API
To automatically display category suggestions, make sure you provide us with the category URL using:
- XML Feed: add the category URL to the XML feed.
- Backend API: add the category URL to the Create, update or patch requests.
And you're set!
Alternatives
If you can't store category URLs in Tweakwise, the category handle function should be implemented. To be able to navigate to a category URL when selecting a category suggestion:
<script>
window.addEventListener("twn.suggestions.ready", function () {
window.tweakwiseSuggestions({
input: "INPUT",
//..
categories: {
handle(event) {
var suggestion = event.data;
// Handle category suggestion here...
}
}
});
});
</script>
Note: if this handle is not implemented, the category suggestions will not be displayed.
Popular ways to determine the URL to a category page are:
- using an endpoint
- using a dictionary or mapping
Using an endpoint
/*
Handle category suggestion click.
navigationlink contains category context, that can be used to fetch the category
*/
function handleCategorySuggestion(suggestion){
if(!suggestion) return false;
if(!suggestion.navigationLink) return false;
if(!suggestion.navigationLink.context) return false;
if(!suggestion.navigationLink.context.category) return false;
const categoryContext = suggestion.navigationLink.context.category;
// because a category can have multiple parents, we always use category paths
// in this case, only the last id in the path is relevant
const path = categoryContext.path.split('-');
const id = path[path.length - 1];
// fetch the associated category from an endpoint in your platform
const url = fetch('/api/categories/'+id).then((response) => response.json())
.then(function(result){
if(result && result.url){
// redirect to category page
location.href = result.url;
} else {
// fallback to search page
location.href = '/search/#twn|?tn_q=' + data.match;
}
});
}
window.addEventListener("twn.suggestions.ready", function () {
window.tweakwiseSuggestions({
input: "#tweakwise-input",
// ...
categories: {
handle(event) {
handleCategorySuggestion(event.data);
}
},
});
})
Using a dictionary or mapping
// generate a category mapping in your platform
const categories_dict = {
'1001': '/clothing',
'1004': '/clothing/sweaters-hoodies',
'1005': '/clothing/shirts',
'1006': '/clothing/dresses',
'1007': '/clothing/blouses',
'1002': '/accessories',
}
/*
Handle category suggestion click.
navigationlink contains category context, that can be mapped to the categoryid + url dict
*/
function handleCategorySuggestion(suggestion){
if(!suggestion) return false;
if(!suggestion.navigationLink) return false;
if(!suggestion.navigationLink.context) return false;
if(!suggestion.navigationLink.context.category) return false;
const categoryContext = suggestion.navigationLink.context.category;
// because a category can have multiple parents, we always use category paths
// in this case, only the last id in the path is relevant
const path = categoryContext.path.split('-');
const id = path[path.length - 1];
// fetch the associated category from the mapping
if(id in categories_dict){
return categories_dict[id];
}
return false;
}
window.addEventListener("twn.suggestions.ready", function () {
window.tweakwiseSuggestions({
input: "#tweakwise-input",
// ...
categories: {
handle(event) {
// find the category url
const url = handleCategorySuggestion(event.data);
if(url){
location.href = url;
} else {
// fallback to search
location.href = '/search/#twn|?tn_q=' + data.match;
}
}
},
});
})