Shopify: Implement on Search Page
Use Search & Discover when the Shopify storefront search returns no products. Shoppers get a Preferabli search pane as a fallback instead of a dead end.
This guide uses two snippets:
- Assets — CSS and scripts, rendered from
layout/theme.liquid - Plugin — Search Discover host and init, rendered on the search empty state
It applies to Online Store 2.0 themes.
What you need
- Preferabli
integration_id(required) - Preferabli
collection_id(recommended for search) - jQuery 3 or later on the page before the plugin script
See Getting Started for a general install. Brand colors and pane layout are CSS variables — see Styling.
Overview
- Create an assets snippet that loads Preferabli CSS and JS from
sd.preferabli.com, and render it fromlayout/theme.liquid. - Create a plugin snippet and render it where the search template shows no results.
- Replace the Preferabli IDs in the plugin snippet.
The plugin does not replace Shopify search. On /search with zero results it adds a Preferabli CTA (or opens the pane) so shoppers can keep looking.
1. Create the assets snippet
Under Snippets, add:
preferabli-search-discover-assets.liquid
{% comment %} Preferabli Search Discover — stylesheet and scripts from sd.preferabli.com. Render once from layout/theme.liquid before </body>.{% endcomment %}
<link rel="stylesheet" href="https://sd.preferabli.com/preferabli.searchdiscover.style.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js" defer></script>
<script src="https://sd.preferabli.com/preferabli.searchdiscover.js" defer></script>
If the theme already loads jQuery globally, remove the jQuery <script> line. The Preferabli script must still run after jQuery.
To load assets only on search pages:
{% if request.page_type == 'search' %}
<link rel="stylesheet" href="https://sd.preferabli.com/preferabli.searchdiscover.style.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js" defer></script>
<script src="https://sd.preferabli.com/preferabli.searchdiscover.js" defer></script>
{% endif %}
Add it to theme.liquid
theme.liquidOpen layout/theme.liquid. Before the closing </body> tag, add:
{% render 'preferabli-search-discover-assets' %}
Example:
{{ content_for_layout }}
{% render 'preferabli-search-discover-assets' %}
</body></html>
2. Create the plugin snippet
Under Snippets, add:
preferabli-search-discover-empty.liquid
{% comment %}
Preferabli Search Discover — empty Shopify search fallback. Render from the search empty state (for example sections/main-search.liquid).
{% endcomment %}
{% if search.performed and search.results_count == 0 %}
<div id="sd-shopify-empty"></div> <script> document.addEventListener('DOMContentLoaded', function () {
if (!window.jQuery || !jQuery.fn.preferabliSearchDiscover) return;
var shopifyQuery = {{ search.terms | json }};
jQuery('#sd-shopify-empty').preferabliSearchDiscover({
integration_id: 'YOUR_INTEGRATION_ID',
collection_id: 'YOUR_COLLECTION_ID',
type: 'search',
query: shopifyQuery,
cta_button: {
label: 'Search with Preferabli',
position: 'inline',
className: 'button',
},
});
});
</script>
{% endif %}
Replace YOUR_INTEGRATION_ID and YOUR_COLLECTION_ID with the merchant’s Preferabli values.
search.terms is the Shopify search string. Pass it as query so Preferabli opens with that term filled in and runs the search (two or more characters).
Some themes use search.results.size == 0 instead of search.results_count == 0. Use whichever the theme already uses.
Add it to the search empty state
Open the search section (Dawn: sections/main-search.liquid). Find the empty-results branch and render the snippet there:
{% if search.performed %}
{% if search.results_count == 0 %}
<h2>{{ 'templates.search.no_results' | t: terms: search.terms }}</h2>
{% render 'preferabli-search-discover-empty' %}
{% else %}
{% comment %} result grid {% endcomment %}
{% endif %}
{% endif %}
position: 'inline' keeps the CTA in the empty-state block. For a floating corner button instead:
cta_button: {
label: 'Search with Preferabli',
position: 'bottom-right',
anchor: 'window',
},
To open the pane as soon as the empty page loads, still pass Shopify’s term:
var shopifyQuery = {{ search.terms | json }};
var $host = jQuery('#sd-shopify-empty');
$host.preferabliSearchDiscover({
integration_id: 'YOUR_INTEGRATION_ID',
collection_id: 'YOUR_COLLECTION_ID',
type: 'search',
query: shopifyQuery,
});
$host.preferabliSearchDiscover('show', {
trigger: 'shopify_search_no_results_auto',
query: shopifyQuery,
});
Full options: Configuration.
3. Match the storefront look
Add brand overrides in the assets snippet, after the stylesheet:
{% style %}
:root {
--sdjs-brand-primary: {{ settings.colors_accent_1 }};
--sdjs-brand-on-primary: #fff; }
{% endstyle %}
Or pass theme button classes through cta_button.className. Details: Styling.
Snippet map
| Snippet | Role | Where to render |
|---|---|---|
preferabli-search-discover-assets | Stylesheet + jQuery + plugin script from sd.preferabli.com | layout/theme.liquid before </body> |
preferabli-search-discover-empty | Host element + Search Discover init | Search empty state (for example sections/main-search.liquid) |
Notes and limits
- Prefill search uses
search.terms. Pass it asqueryat init and/or onshow. - Assets must load before init. Keep the assets snippet in
theme.liquidso scripts are on the page when the empty-state snippet runs. - Predictive search is separate. Header typeahead is not the results page. The plugin snippet only targets
/searchwith zero results. - Search apps. If an app replaces the search results page, render
preferabli-search-discover-emptyfrom that app’s empty-state Liquid instead.
Updated 12 days ago
