I want to run methods from the SDK for example I want the cart to programmatically be populated with an array of items. The only way I seem able to access the Snipcart object is by directly referencing window. With a Gatsby project when I try to deploy and build it with Netlify it breaks because it can not access window, “window is not available during server side rendering.”
Is there any work around to this or way to access Snipcart object without referencing window?
Thanks so much. I’ve pasted an example bit of code in which I’m referencing the snipcart object with window:
const addCart = async () => {
try {
if (filteredProducts.length > 0) {
await filteredProducts.forEach(product => {
window.Snipcart.api.cart.items.add({
id: product.node.id,
name: product.node.name,
price: product.node.price,
url: `https://modest-curran-c90323.netlify.app/shop/${product.node.slug}`,
quantity: 1,
})
})
} else {
return
}
} catch (e) {
console.log(e)
}
}