Build Headless commerce interfaces with Shopify Liquid
Build Headless commerce interfaces with Shopify Liquid
If you use a Shopify Liquid theme, we typically recommend using Coveo Atomic. The Coveo Atomic components are pre-built and let you quickly create product discovery interfaces that are compatible with Shopify Liquid.
However, if you want to create a more customized interface, you can use the Coveo Headless library with Shopify Liquid.
We typically recommend using Headless with Shopify Hydrogen to create a high-performance, customizable search interface. However, you can also use it with Shopify Liquid if you have a legitimate reason to use a Liquid theme. For example, you might already use a React-based component library embedded in your Shopify Liquid theme. In this case, you can integrate Coveo Headless into that library instead of starting a new project from scratch with Shopify Hydrogen. This article provides a high-level overview of how to use Coveo Headless with Shopify Liquid.
Headless commerce usage
|
When working with Shopify Liquid, initialize your engine using |
Build the commerce engine
|
All controllers depend on a single |
import { buildShopifyCommerceEngine } from '@coveo/shopify/headless/commerce';
export const getEngine = () => {
if (_engine !== null) {
return _engine;
}
_engine = buildShopifyCommerceEngine({
commerceEngineOptions: {
configuration: {
accessToken: '<ACCESS_TOKEN>',
organizationId: '<ORGANIZATION_ID>',
analytics: {
enabled: true,
trackingId: '<TRACKING_ID>'
},
context: {
country:{{ localization.country.iso_code | json }},
currency: {{ localization.country.currency.iso_code | json }},
language: {{ request.locale.iso_code | json }},
view: {
url: {{ canonical_url | json }}
},
cart: {{ cart.items | json }}.map(function (item) {
return {
productId: `gid://shopify/ProductVariant/${item.variant_id}`,
name: item.title,
price: item.final_price,
quantity: item.quantity,
};
}),
}
},
},
});
return _engine;
};
An access token with the Search - Execute queries - Allowed and Analytics - Analytics data - Push privileges. | |
Your organization ID. | |
The target tracking ID. | |
The context object containing the country, currency, view,language, and cart information, extracted from the Shopify store. |
Initialize the web pixel
The Coveo app web pixel logs cart, product view, and purchase events.
|
If you’re not using the Coveo AI Search and Discovery app and don’t have the Coveo app web pixel installed, you’ll need to create your own. |
To ensure that Headless and the web pixel are synchronized across your entire store, create an initialization snippet and include it in every page of your Shopify store.
-
Create a snippet named
initialize-analytics.liquid
and paste the following code:<!-- snippets/initialize-analytics.liquid --> <script type="module"> import {init, fetchAppProxyConfig} from 'https://static.cloud.coveo.com/shopify/v1/headless/commerce.esm.js'; const config = await fetchAppProxyConfig({
marketId: {{ localization.market.id | encode_url_component }}, }); init(config);
</script>
The Coveo app contains an App Proxy endpoint that returns an object containing the access token, organization ID, client ID, and tracking ID, which are required to initialize the web pixel. If you’re not using the Coveo app, pass the accessToken
,organizationId
,clientId
, andtrackingId
parameters directly to theinit
function below.The init
function initializes the web pixel (see @coveo/shopify). -
Include this snippet in your
theme.liquid
file, just before the closing</head>
tag:{% render 'initialize-analytics' %}
Create UI components
If you’re using Coveo Headless with Shopify Liquid, you’ve likely implemented your Shopify Liquid theme using a framework such as React, Vue, or Angular.
To build UI components within that framework, see Headless commerce usage (client-side rendering).