Shopify Buy SDK: TypeError: Cannot read property 'checkoutLineItemsAdd' of undefined

New Member
2 0 0
I'm having trouble adding a product to cart via the Shopify buy SDK.
 

In this example code:

shopClient.checkout.create().then((checkout) => {

    const checkoutId = checkout.id; 

    const lineItemsToAdd = [{        variantId : someVariantId, 
        quantity : parseInt(quantity)
    }];
    shopClient.checkout.addLineItems(checkoutId, lineItemsToAdd).then((checkout) => {      console.log(checkout.lineItems)
    });

});

When trying to add a line item to the existing checkout I get:

 

TypeError: Cannot read property 'checkoutLineItemsAdd' of undefined

 

I've tried using the actual product id instead of a variant id. Also I've tried using the variant id as is from the graph ql response (in Base64) and converting it to a number, but no luck.

 

I would appreciate any help

 

Thanks

0 Likes
Highlighted
Shopify Expert
9965 84 1469

Can you post the variantId you are using for a sanity check?

You should be using the base64 encoded string, which decoded would look like 

gid://shopify/ProductVariant/123456789
★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
0 Likes
New Member
2 0 0

Hi, thanks for your answer. 

I've tried using the variant id in the following ways:

Base 64 version: Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC8yOTcxMjI5MTU5NDMxMg==

Decoded version: gid://shopify/ProductVariant/29712291594312

And only the numeric part: 29712291594312

 

No luck

0 Likes
New Member
1 0 0

I was facing this problem today using the Storefont API, but solved it using the Base64 encoded string of the `graphql_api_id` property which is associated with the variant, which (as described above) look like this: gid://shopify/ProductVariant/12434084921391.

 

You can get these from product objects, but you can also create them directly from your numeric variantId.

 

// this.checkoutId below is created after the client.checkout.create() promise earlier in the code

addLineItem(variantId, quantity = 1) { const graphqlApiId = window.btoa(`gid://shopify/ProductVariant/${variantId}`) const lineItemsToAdd = [{ variantId: graphqlApiId, quantity, }] // Add an item to the checkout this.client.checkout.addLineItems(this.checkoutId, lineItemsToAdd) .then((checkout) => { console.log(checkout.lineItems) }); }

This worked perfectly and I got back a GraphModel objects.

 

Hope that helps.

0 Likes