Docs
STEP 2-2

Step 2: Implement CartConnector

Implement the two functions that the SDK uses to fetch and update the cart (getCart and updateCheckoutPlusLines) with your existing cart logic. The object with these two functions is CartConnector, which you pass to cart in init().

Why a CartConnector is needed

The SDK reads and writes your cart when it determines the price and when the shopper proceeds to checkout.

FunctionWhen the SDK calls itPurpose
getCartOn the first mount() or refresh(), when refresh() notifies it of a cart change, when the previous screen is shown as is, and inside prepareCheckout() (before and after adding)Read the benefit set (the commitment charge and benefits for each range) from the Checkout+ metafield, and determine the price from the cart amount and product tags. Check whether a Checkout+ product added on a previous page is left in the cart. After adding, confirm that it made it into the cart
updateCheckoutPlusLinesOn the first mount() or refresh() and when the previous screen is shown as is, if the SDK finds a leftover Checkout+ product. Inside prepareCheckout()Add or remove only Checkout+ product lines. It doesn't touch any other lines

With a Shopify theme, every Shopify store reads and writes the cart the same way, so the app embed reads and writes the cart directly. In headless Shopify stores, where the cart ID is stored, how the Storefront API is called (access token, API version, the client you use), and the cart state your UI holds all differ from Shopify store to Shopify store. So the SDK doesn't call the Storefront API directly; instead, you implement only the read and write parts with your existing cart logic. You don't need to pass an access token to the SDK, and your cart logic stays in one place in your code.

The SDK is responsible for deciding when to add or remove what, checking the result after adding, and merging lines when two Checkout+ product lines end up in the cart. CartConnector should fetch and update exactly as the SDK instructs and return the results as they are. After the SDK adds or removes a Checkout+ product, it notifies you through onCheckoutPlusLinesChange, so refetch the cart for your UI (Step 3: Initialize the SDK).

Specification

What CartConnector follows is in What to follow for getCart, and in Which cart to update and What to return for updateCheckoutPlusLines. The fields of the return value and the argument are on those pages too.

Example implementation

The cart ID includes a secret part (everything from ?key=; see Cart ID in Shopify's documentation). Take care with how you handle it in your implementation.

Below are examples of the Storefront API query and mutations used for getCart and updateCheckoutPlusLines. Call them from your cart logic.

Query for getCart

Fetch all lines of the cart and the Checkout+ metafield in one query. Because it reads product tags and the metafield, call it with an access token that has the permissions in Preparation.

query CheckoutPlusCart($cartId: ID!, $after: String, $namespace: String!, $key: String!) {
  shop { metafield(namespace: $namespace, key: $key) { value } }
  cart(id: $cartId) {
    cost { totalAmount { currencyCode } }
    lines(first: 250, after: $after) {
      pageInfo { hasNextPage endCursor }
      nodes {
        id
        quantity
        attributes { key value }
        cost { totalAmount { amount } }
        merchandise { ... on ProductVariant { id product { handle title tags } } }
      }
    }
  }
}
  • For namespace and key, pass the namespace and key of the Checkout+ metafield that Recustomer gives you.
  • While cart.lines.pageInfo.hasNextPage in the response is true, call it again with endCursor as after to fetch the remaining lines.
  • If the response has errors, throw an exception even when data is returned (What to follow for getCart).
  • If it has no errors, put the values from the response into the return value as shown in the table in Return value of getCart. If cart in the response is null, return null.
  • In Hydrogen, passing cache: storefront.CacheNone() makes the call without a cache.

Mutations for updateCheckoutPlusLines

mutation CheckoutPlusLinesRemove($cartId: ID!, $lineIds: [ID!]!) {
  cartLinesRemove(cartId: $cartId, lineIds: $lineIds) { cart { id } userErrors { message } }
}

mutation CheckoutPlusLinesAdd($cartId: ID!, $lines: [CartLineInput!]!) {
  cartLinesAdd(cartId: $cartId, lines: $lines) { cart { id } userErrors { message } warnings { code } }
}
  • Remove the lines in change.remove with cartLinesRemove, and add the product in change.add with cartLinesAdd. For lines in cartLinesAdd, pass one line with change.add.variantId as merchandiseId and the quantity and attributes from change.add.
  • What to return for userErrors and stock warnings is in What to return for updateCheckoutPlusLines.
  • The cart ID can change with every update (Shopify's Hydrogen example). Continue with cart.id from the response, and save it again the same way your cart logic does. If an update fails partway and you throw, save the cart.id that changed in the earlier responses before you throw.
© Recustomer, Inc.