This guide will walk you through Purple Dot's Platform integration into any Shopify Hydrogen Storefront. If you are using our Shopify App, please seeInstalling our Shopify App instead. See Purple Dot Overview for the comparison of our Shopify App vs. Platform integrations.
In your .env file and in remix.env.d.ts, add PURPLE_DOT_API_KEY and set it to the public API key you can find in the integration page of your Purple Dot merchant portal. You must also have PUBLIC_STOREFRONT_API_TOKEN and PUBLIC_STORE_DOMAIN set.
Initialise the package
Before you can use the package, it needs to be initialised with your store's apiKey and the Storefront cart adapter. This needs to be initialised on each loader that requires the use of the package so that you can use the package in your backend handlers as well as React components:
// Server-side
import * as PurpleDot from '@purple-dot/browser';
import { ShopifyStorefrontCart } from '@purple-dot/browser/shopify-storefront-cart';
export async function loader({context}: LoaderArgs) {
const publicStoreDomain = context.env.PUBLIC_STORE_DOMAIN;
const publicStorefrontApiToken = context.env.PUBLIC_STOREFRONT_API_TOKEN;
const purpleDotApiKey = context.env.PURPLE_DOT_API_KEY;
PurpleDot.init({
apiKey: purpleDotApiKey,
cartAdapter: new ShopifyStorefrontCart(
publicStoreDomain,
publicStorefrontApiToken,
),
});
// Use Purple Dot SDK...
// Return data to page
return defer(
{
// ...
publicStoreDomain,
publicStorefrontApiToken,
purpleDotApiKey,
},
{headers},
);
}
// Client-side
import * as PurpleDot from '@purple-dot/browser';
import { ShopifyStorefrontCart } from '@purple-dot/browser/shopify-storefront-cart';
export default function App() {
const data = useLoaderData<typeof loader>();
useEffect(() => {
PurpleDot.init({
apiKey: data.purpleDotApiKey,
cartAdapter: new ShopifyStorefrontCart(
data.publicStoreDomain,
data.publicStorefrontApiToken,
),
});
}, []);
return (
<html lang="en">...</html>
);
}
Set up product pages
On every product form, two elements are expected if a variant/product is on preorder:
The waitlist shipping dates so that shoppers know when to expect their order to start shipping
A Purple Dot learn more element so that shoppers can understand their preorder process with Purple Dot
A non-preorder product:
A pre-order product:
In the following example, we are going to fetch the preorder data in the loader:
Set the right line item properties when adding to cart
The Purple Dot package expects the __releaseId property to exist on the line item to detect preorders in the shopper’s cart. You can easily add this by modifying your add to cart calls. Here’s how you can do it using the standard AddToCartButton element.
You can use the Purple Dot Preorder property that you added in your line item to differentiate preorder items in the side cart. For example, with a CartLineItem.tsx file that contains the component to display each line item in the shoppers, here’s how you can do it:
Once the shoppers have added their preorder items to the cart, it’s time to let them checkout through the Purple Dot checkout. You can do this on the cart page as shown below:
// app/pages/cart.tsx
import * as checkout from '@purple-dot/browser/checkout';
import { cartHasPreorderItem } from '@purple-dot/browser/cart';
export default function Cart() {
const [root] = useMatches();
const cart = root.data?.cart as Promise<CartApiQueryFragment | null>;
return (
<div className="cart">
<h1>Cart</h1>
<Suspense fallback={<p>Loading cart ...</p>}>
<Await errorElement={<div>An error occurred</div>} resolve={cart}>
{(cart) => {
// Open Purple Dot checkout if the cart contains a pre-order
useEffect(() => {
if (cart) {
cartHasPreorderItem(cart.lines.nodes).then((hasPreorderItem) => {
if (hasPreorderItem) {
checkout.open({
cartId: cart.id,
});
}
});
}
}, [cart?.id]);
return <CartMain layout="page" cart={cart} />;
}}
</Await>
</Suspense>
</div>
);
}
This opens up an iframe that allows the shopper to checkout with Purple Dot.
Embed the Purple Dot self-service for shoppers to manage their preorders
Pick any page where you’d like to display the Purple Dot self-service and embed the Purple Dot self-service as shown below: