Error Message with AddLineItems - please help

Highlighted
New Member
1 0 0

Hello, I keep receiving an error message (Uncaught TypeError: Cannot read property 'addLineItems' of undefined) each time I try to add a Line Item. I've found various suggestions for fixing the problem but nothing works. Please help.

 

function activateshop() {
  shopClient.fetchAllProducts()
    .then((products) => {
      // Pass Products to the buildProducts function
      buildProducts(products)
      productBucket.push(products)
      console.log("Products Pulled")
    })
    .then(() => {
      if (cartCreated == false) {
        shopClient.createCart().then((checkout) => {
          console.log("Cart Created");
          cartCreated == true;
          var addToCart = document.getElementById("addcart");
          // Add Items to Cart 
          addToCart.addEventListener("click", function add() {
            var topCartNum = document.getElementById("cartamount");
            var newl = window.btoa(`gid://shopify/ProductVariant/${selectedProd.variant}`);
            //
            const checkoutId = window.btoa(`gid://shopify/ProductVariant/${checkout.id}`);
            const lineItemsToAdd = [
              {
                variantId: newl,
                quantity: 1,
                customAttributes: [{ key: "MyKey", value: "MyValue" }]
              }
            ]
            topCartNum.innerHTML = checkout.lineItemCount
            // No items added Error
            if (selectedProd.empty == true) {
              document.getElementById("Shoperror").style.display = "block";
              document.getElementById("Shoperror").style.transition = "all .5s";
              setTimeout(function () { document.getElementById("Shoperror").style.display = "none"; }, 1800)
            }
            console.log(lineItemsToAdd)
            shopClient.checkout.addLineItems(checkoutId, lineItemsToAdd).then((checkout) => {
              // Do something with the updated checkout
              console.log(checkout.lineItemsToAdd); // Array with one additional line item
            })
          }
          )
        })
      }
    })
    .catch(() => {
      console.log('Request failed');
    })
}
0 Likes
Shopify Staff
Shopify Staff
268 36 38

Hey @sagent,

 

It's difficult to tell what's going on without seeing all of the code. Try reducing the code to the bare minimum (create checkout, add items) and working backwards from there.

 

It could just be that you haven't created a checkout yet:

 

client.checkout.create().then((checkout) => {
  // Now try client.checkout.addLineItems
});

 

0 Likes