@shopify/theme-predictive-search
v4.1.1
Published
Shopify AJAX Predictive Search API
Downloads
7,542
Readme
Shopify Predictive Search JS API
Configuration options
See the help docs for Predictive Search for more information.
Getting started
import PredictiveSearch from "@shopify/theme-predictive-search";
var predictiveSearch = new PredictiveSearch({
search_path: PredictiveSearch.SEARCH_PATH,
resources: {
type: [PredictiveSearch.TYPES.PRODUCT],
limit: 4,
options: {
unavailable_products: "last",
fields: [
PredictiveSearch.FIELDS.TITLE,
PredictiveSearch.FIELDS.PRODUCT_TYPE,
PredictiveSearch.FIELDS.VARIANTS_TITLE
]
}
}
});
predictiveSearch.on("success", function(json) {
// See "Success Response" section of this document
});
predictiveSearch.on("error", function(error) {
// See "HTTP status `3xx-4xx` Response" section of this document
});
predictiveSearch.query("Yeti SB165");
Success Response
// JSON Output
{
"resources": {
"results": {
"products": [
{
"available": true,
"body": "<p>The best bike ever made</p>",
"compare_at_price_max": "5199.99",
"compare_at_price_min": "4899.99",
"handle": "yeti-sb165",
"id": 111,
"image": "https://cdn.shopify.com/s/...",
"price": "4999.99",
"price_max": "5199.99",
"price_min": "4899.99",
"tags": ["best", "bike", "ever", "made"],
"title": "Yeti SB165",
"type": "best-bike",
"url": "/products/sb165",
"variants": [
{
"available": true,
"compare_at_price": "9999.99",
"id": 222,
"image": "https://cdn.shopify.com/s/...",
"price": "9999.99",
"title": "Yeti SB165 X01 build",
"url": "/products/sb165/x01"
}
],
"vendor": "Yeti Cyclesr"
}
]
}
}
}
HTTP status 3xx-4xx
Response
predictiveSearch.on("error", function(error) {
console.error(error.status); // Ex. output: 429
console.error(error.name); // Ex. output: Throttled
console.error(error.message); // Ex. output: Too Many Requests
// When the request response HTTP status is 429
console.error(error.retryAfter); // Ex. output: 100
});