Need help with adding items to the cart with the JS Buy SDK

Highlighted
New Member
2 0 0

Hey there,

I'm trying to build a custom Add To Cart button + Cart with the JS Buy SDK. The problem I am currently facing is that I am not able to add products to the cart. 

This is how my code looks so far. (I also don't really understand the variantID and checkoutID.

 

 

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

    totalElement.innerHTML = checkout.totalPrice

    const handle = "bal-side-table";

    client.product.fetchByHandle(handle).then((product) => {

        cartButton.addEventListener("click", function() {

            const checkoutId = checkout.handle;

            const lineItemsToAdd = [{
                variantId: product[0].variants[0].handle,
                quantity: 1,
            }];

            client.checkout.addLineItems(checkoutId, lineItemsToAdd).then((checkout) => {
                console.log(checkout.lineItems);
            });

        });

    });


});

 

 

It would be super nice if someone could help me here.

Thanks very much in advance!!

0 Likes
Highlighted
Shopify Partner
529 38 109

While I'm not familiar with the JS Buy SDK, I'd think that the handle for these two fields would be different than their actual unique identifiers (aka ID's). So I'd suppose that they would be referenced like:

const checkoutId = checkout.id;

and

 

const lineItemsToAdd = [{
                variantId: product[0].variants[0].id,
                quantity: 1,
            }];
1 Like
Highlighted
New Member
2 0 0

Thanks! That helped  

0 Likes