Add an item to the cart with AJAX

Add item to cart in BigCommerce

This creates the createCart function using Fetch.

function createCart(url, cartItems) {
   return fetch(url, {
       method: "POST",
       credentials: "same-origin",
       headers: {
           "Content-Type": "application/json"},
       body: JSON.stringify(cartItems),
   })
   .then(response => response.json());
 };
createCart(`/api/storefront/carts`, {
   "lineItems": [
   {
       "quantity": 1,
       "productId": 86
   }
   ]}
)
.then(data => console.log(JSON.stringify(data)))
.catch(error => console.error(error));

Need Help? Contact Us