That seems like a good solution! I am still relatively new to Javascript, I tried including the following into my js file that gets loaded with every page.
Is this the right way to go about it? At first, I simply had the try/catch block in the cart.created event listener, but would get an error saying I couldn’t get an error saying
await is only valid in async functions and the top level bodies of modules
So I created the fillBill
function outside and calling that on cart.created. Seems to work well! I still need to try hiding with CSS and disabling shipping, but have to run for now.
Thanks for the help!
async function fillBill() {
try {
await Snipcart.api.cart.update({
email: 'john.doe@example.com',
metadata: {
customMetadataKey: 'value'
},
billingAddress:{
name: 'John Doe',
address1: '3671 Garfield Road',
city: 'Neponset',
country: 'US',
province: 'IL',
postalCode: '61345'
}
});
} catch (error) {
console.log('error occurred');
console.log(error);
}
}
document.addEventListener('snipcart.ready', () => {
console.log("snipcart ready");
Snipcart.events.on('item.added', (cartItem) => {
console.log("item added")
console.log(cartItem);
});
Snipcart.events.on('cart.created', (cartState) => {
console.log("cart created")
console.log(cartState);
fillBill();
});
});