Adding multi-shop cart pages
You've created a marketplace-level cart icon that displays the total number of items in the cart. Now you want buyers to be able to see and edit what's in the cart, across all shops. The cart page should direct buyers towards a sale.
In this tutorial, you'll learn how to create an interactive cart page at the marketplace-level. A marketplace-level cart page is one that displays all products in the cart, across all shops. The page will include a GO TO CHECKOUT button for each product, which redirects buyers from the cart to the relevant online store for purchase.
What you'll learn
Anchor link to section titled "What you'll learn"After you've finished this tutorial, you'll know how to do the following:
Filter shops by domain
Build a main cart page with a button redirecting users to an online store's checkout
Add the ability to edit and remove items from the cart
Requirements
Anchor link to section titled "Requirements"- You've completed the tutorial for adding a marketplace-level cart.
Step 1: Build the main cart page
Anchor link to section titled "Step 1: Build the main cart page"Retrieve the carts that you stored in local storage, using the helper that you created.
Make a request to the channel app's GraphQL API for the Storefront API access tokens for all shops in the cart.
The following request passes a
domains
argument to theshops
query to return only the shops that match a list of domains. You can use the domain and Storefront API access token to create an Apollo Client for each shop in the cart.Query the Storefront API's
Cart
object for the cart details, and use this information to build out the cart page.The following example uses the
inContext
directive, to ensure a consistent currency for all shop carts.Build the Cart page, including a GO TO CHECKOUT button for each cart.
The button redirects buyers to the cart's checkout URL in a new tab.
Build a CART SUMMARY that returns a summary of all the carts.
The cart page might now look something like the following:
Step 2: Add edit and delete functionality
Anchor link to section titled "Step 2: Add edit and delete functionality"In this step, you'll add the ability for buyers to edit item quantities and remove items from the cart entirely.
In
helpers/cartHelpers.js
, create the following mutations using the Storefront API's Cart object.cartLinesRemove
enables buyers to delete line items from the cart.cartLinesUpdate
is what buyers use to edit the quantity of a line item in the cart.
Similar to the cart query that you created, you can use the
inContext
directive to ensure cart prices are returned in a consistent currency.To ensure that the same
cart
fields are returned for these mutations and for queries, you'll use a GraphQL fragment. The fragment should contain the same fields that you used in the main cart page query.Update the
CART_DETAILS_QUERY
to use theCORE_CART_FIELDS
fragment that you created.Since the fields in the fragment are the same as in the query that you created, you don't need to make any changes to code that uses the query response data.
The cart page might now look something the following:
Step 3: Refresh the page after checkout
Anchor link to section titled "Step 3: Refresh the page after checkout"When a buyer completes the checkout for a specific shop, the marketplace's cart page should indicate this by removing the shop's items from the cart page and displaying a message.
In
helpers/cartHelpers.js
, in theaddToExistingCart
function, add code to create a new cart if thecartLinesAdd
mutation returnsnull
.Receiving a
null
cart on the mutation indicates that the checkout is complete, and that a new cart will be created if the buyer goes back and adds another item from the same shop post-checkout.You can also add a helper that will remove a shop's cart from local storage, so that you can remove the cart after checkout is complete.
When checkout is complete, the shop section on the cart page might look something like the following:
Learn how to onboard merchants to your channel app.
Share feedback on Marketplace Kit.