Jump to content

How to access squarespace commerce/shopping-cart api

Recommended Posts

Hello. I am trying to make a custom shopping experience where I have too many variants to fit into one product. There are ~5 different categories each with 10 options each. So we decided to split the products up into different "sub" products. In doing so, I need to create a mini web app that allows the user to choose which items they want to add to the cart culminating into the end where when the user clicks the finish button, the button will trigger an event that will add all 5 products to the cart at once for the user. I however cannot find documentation for the api used by squarespace (api/commerce/shopping-cart/entries) to perform such actions. In essence, I want to be able to programatically access and edit the shopping cart via a code block on a certain page on my website. I know that we cannot add code the checkout page itself, but I just want to be able to edit the cart on other parts of the website. Thanks

Link to comment
  • 2 years later...

Using 7.0 this is what I came up with:

This adds to cart multiple item ids.

You could hard code the crumb, instead of searching for it.
Make sure you have the correct item ids for each of those items.

This does not update your shopping cart pill / totals.

function getCookie(name) {
    const nameEQ = name + "=";
    const ca = document.cookie.split(';');
    for(let i = 0; i < ca.length; i++) {
      let c = ca[i];
      while (c.charAt(0) === ' ') {
        c = c.substring(1, c.length);
      }
      if (c.indexOf(nameEQ) === 0) {
        return decodeURIComponent(c.substring(nameEQ.length));
      }
    }
    return null;
}
 
async function postData(url = '', data = {}) {    
    const response = await fetch(url, {
      method: 'POST',
      mode: 'cors',
      cache: 'no-cache',
      credentials: 'same-origin',
      headers: {
        'Content-Type': 'application/json'  
      },
      redirect: 'follow',
      referrerPolicy: 'no-referrer',
      body: JSON.stringify(data)
    });
    return response.json();
}
 
var baseurl = "https://yourwebsite.com";
var path = "/api/commerce/shopping-cart/entries";
var crumb = getCookie("crumb");
 
function buildUrl(baseurl, path, parameter){
    return baseurl + path + "?" + "crumb=" + parameter;
}
 
function addToCart(url, itemId) {
  postData(url, {itemId: itemId, sku: null, additionalFields: "null"})
    .then(data => console.log(data))
    .catch(error => console.error('Error:', error));
}
 
var bundledItems = ["4325345", "25324534", "23453425"];
 
var addToCartURL = buildUrl(baseurl, path, crumb);
bundledItems.forEach(item => {
    addToCart( addToCartURL , item)
});
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.