Rob
Rob

Reputation: 1636

Shopify Storefront API access cart created in web store

So... I've been playing around with Shopify's Storefront API. I have a store... when something is added to the cart, I receive a token which I assume is the cart ID. I log this ID out to my browser console and copy it.

I've made a private app, and I can query products over the Storefront API - so I thought I'd have a go at retrieving a cart by its ID (the one I copied from the browser).

I encode the cartId in base64... just because other examples were base64 strings. Sure enough the ID is correctly decoded in the error message:

{
  "errors": [
    {
      "message": "Invalid global id '76f8efad975def03680e8de12442a65e'",
      "locations": [
        {
          "line": 2,
          "column": 5
        }
      ],
      "path": [
        "query",
        "node",
        "id"
      ],
      "extensions": {
        "code": "argumentLiteralsIncompatible",
        "typeName": "CoercionError"
      }
    }
  ]
}

I don't know what the 'global id' is - but here is my query.


const cartId = 'NzZmOGVmYWQ5NzVkZWYwMzY4MGU4ZGUxMjQ0MmE2NWU'

   const query = `{
    node(id: "${cartId}") {
      id
      ... on Checkout {
        id
        ready
        currencyCode
        subtotalPrice
        taxesIncluded
        totalTax
        totalPrice
        lineItems(first: 250) {
          edges {
            node {
              id
              title
              quantity
              variant {
                id
                price
              }
            }
          }
        }
      }
    }
  }`

Is my syntax correct? Or, am I barking up the wrong tree trying to retrieve a cart that was created in website store land, and not Storefront API land?

If it is possible for my app to access any carts created by visitors to the Shopify site by querying the cart token/id, that would be a game changer.

** Edit **

I refactored my code - so it uses a webhook to grab the cart ID.

But I receive the following error:

'Invalid global id 981820079255243500

I read somewhere (I forget where - its been a rabbit hole) that Storefront API uses a different ID for accessing the same cart to the one provided by the webhook? Is that true, and if so - how do I access it?

import express from 'express';
import bodyParser from 'body-parser';

import fetch from 'node-fetch';
import Client from 'shopify-buy';
global.fetch = fetch;

// ........ removed for brevity


const client = Client.buildClient({
  domain: shopUrl,
  storefrontAccessToken: accessToken
});

// we need to base64 our ID for storefront api
const base64Encode = (data) => {
  const buff = new Buffer(data);
  return buff.toString('base64');
}


const getCart = async (cartId) => {
try {
  const cartBuffer64 = base64Encode(cartId);
  const myCart = await client.checkout.fetch(cartBuffer64)
  console.log(myCart)
  } catch(e) {
    console.log(e)
    throw e
  }
}

// noddy express app to test if this works
app.post('/hook', (req, res) => {
  // todo: validate webhook
  console.log(req.body.id)
  getCart(req.body.id)
  return res.status(200).end()
});

Upvotes: 1

Views: 2282

Answers (1)

David Lazar
David Lazar

Reputation: 11427

You can send any created carts to your App. One, you can assign the webhook to cart/create, with cart/update for changes, or two, you could send the ID/token to an App Proxy. Anything you do with StorefrontAPI, Where you connect it to this App, could also provide info. So you have a lot of choices here. Just be careful about your security.

Upvotes: 1

Related Questions