|
| 1 | +## Shopify Predictive Search JS API |
| 2 | + |
| 3 | +### Configuration options |
| 4 | + |
| 5 | +<table> |
| 6 | + <tr> |
| 7 | + <th width="30%">Type of search</th> |
| 8 | + <th>Options</th> |
| 9 | + </tr> |
| 10 | + <tr> |
| 11 | + <td><code>resources</code></td> |
| 12 | + <td> |
| 13 | + <table> |
| 14 | + <tr> |
| 15 | + <th width="30%">Option</th> |
| 16 | + <th>Type</th> |
| 17 | + <th>Description</th> |
| 18 | + </tr> |
| 19 | + <tr> |
| 20 | + <td><code>types</code> (required)</td> |
| 21 | + <td>Array</td> |
| 22 | + <td>Specifies the type of results requested. Currently supports <code>product</code> only.</td> |
| 23 | + </tr> |
| 24 | + <tr> |
| 25 | + <td><code>fuzzy</code> (optional)</td> |
| 26 | + <td>Boolean</td> |
| 27 | + <td>Enables typo tolerance when searching. A typo will be tolerated after the fourth character. Default: <code>true</code>.</td> |
| 28 | + </tr> |
| 29 | + <tr> |
| 30 | + <td><code>unavailable_products</code> (optional)</td> |
| 31 | + <td>String</td> |
| 32 | + <td>Specifies whether to display <code>unavailable_products</code> results. The three possible options are <code>show</code>, <code>hide</code>, and <code>bury</code>. Burying out of stock products pushes them below other matching results. Default: <code>show</code>.</td> |
| 33 | + </tr> |
| 34 | + <tr> |
| 35 | + <td><code>limit</code> (optional)</td> |
| 36 | + <td>Integer</td> |
| 37 | + <td>Limits the number of results returned. Default: <code>10</code>. Min: <code>1</code>. Max: <code>10</code>.</td> |
| 38 | + </tr> |
| 39 | + </table> |
| 40 | + </td> |
| 41 | + </tr> |
| 42 | +</table> |
| 43 | + |
| 44 | +See the [help docs for Predictive Search](https://help.shopify.com/en/themes/development/predictive-search) for more information. |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +### Getting started |
| 49 | + |
| 50 | +```javascript |
| 51 | +import PredictiveSearch from "@shopify/theme-predictive-search"; |
| 52 | + |
| 53 | +var predictiveSearch = new PredictiveSearch({ |
| 54 | + resources: { |
| 55 | + fuzzy: true, |
| 56 | + types: [PredictiveSearch.TYPES.PRODUCT], |
| 57 | + limit: 4, |
| 58 | + unavailable_products: "bury" |
| 59 | + } |
| 60 | +}); |
| 61 | + |
| 62 | +predictiveSearch.on("success", function(json) { |
| 63 | + // See "Success Response" section of this document |
| 64 | +}); |
| 65 | + |
| 66 | +predictiveSearch.on("error", function(error) { |
| 67 | + // See "HTTP status `3xx-4xx` Response" section of this document |
| 68 | +}); |
| 69 | + |
| 70 | +predictiveSearch.query("The Calling"); |
| 71 | +``` |
| 72 | + |
| 73 | +--- |
| 74 | + |
| 75 | +### Success Response |
| 76 | + |
| 77 | +```json |
| 78 | +// JSON Output |
| 79 | +{ |
| 80 | + "resources": { |
| 81 | + "results": { |
| 82 | + "products": [ |
| 83 | + { |
| 84 | + "id": 1, |
| 85 | + "title": "The Calling", |
| 86 | + "body": "<p>The Calling</p>", |
| 87 | + "handle": "calling", |
| 88 | + "image": "https://cdn.shopify.com/...", |
| 89 | + "type": "Bikes", |
| 90 | + "tags": ["bike", "dolphin"], |
| 91 | + "url": "/products/calling?variant_id=1", |
| 92 | + "price": "3099", |
| 93 | + "available": true, |
| 94 | + "variants": [ |
| 95 | + { |
| 96 | + "title": "Large / Angry Dolphin", |
| 97 | + "url": "https://www.evil-bikes.com/products/calling", |
| 98 | + "image": "https://cdn.shopify.com/...", |
| 99 | + "price": "3099", |
| 100 | + "compare_at_price": "4099", |
| 101 | + "available": true, |
| 102 | + } |
| 103 | + ] |
| 104 | + } |
| 105 | + ] |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +### HTTP status `3xx-4xx` Response |
| 112 | + |
| 113 | +```js |
| 114 | +predictiveSearch.on("error", function(error) { |
| 115 | + console.error(error.status); // Ex. output: 429 |
| 116 | + console.error(error.name); // Ex. output: Throttled |
| 117 | + console.error(error.message); // Ex. output: Too Many Requests |
| 118 | + |
| 119 | + // When the request response HTTP status is 429 |
| 120 | + console.error(error.retryAfter); // Ex. output: 100 |
| 121 | +}); |
| 122 | +``` |
0 commit comments