I have spent several days and lost a lot of hair trying to get the onCartCreated in wix-stores-backend to work.
I have read many “solutions” on line and it just doesn’t seem to fire - EVER!
To start with I just want something simple to prove that the event actually fires;
import wixData from ‘wix-data’;
import wixStores from ‘wix-stores-backend’;
export function wixStores_onCartCreated(event) {
//let total = event.totals.total;
wixData.insert(‘backendLog’, {‘data’: ‘add’});
}
This code is in events.js in the backend folder.
The collection looks like this;
Hi - I tried this and no console message was being displayed which suggests that this code is not being called.
I have read many articles about this and unless I am missing something fundamental in the GUI I cannot fathom why it doesn’t work. @Yisrael I will try the monitoring tool to see if anything does actually fire - thanks
@yisrael-wix It appears that this tool will monitor events from objects on a page. I am trying to hook into events in the backend.
not only have I tried to do everything above and I have tried other spurious events such as a beforequery on my data collection but nothing seems to fire.
I really want to add some backend code when something is added to my shopping cart, but I can’t get it to work for love nor money!
I can get events to work of page objects though, but this is no good to me
@givemeawhisky I tried this and no console message was being displayed which suggests that this event code is not being called.
I have read many articles about this and unless I am missing something fundamental in the GUI I cannot fathom why it doesn’t work.
Richard, try doing something like this with your code:
import wixData from 'wix-data';
import wixStores from 'wix-stores-backend';
export function wixStores_onCartCreated(event) {
let total = event.totals.total;
wixData.insert('backendLog', {'title': total})
}
Also, strange as it might seem, some people have suggested renaming the events.js in your backend to eventS.js and then sync to live and publish your site again.
Finally, make sure that your dataset collection permissions are set for everyone and that you are testing it on your live published site and not through the preview sandbox mode.
Cross fingers that when you now add something to your cart, something should be happening now
@richardmetters The Site Monitoring feature is able to log from anywhere in your site code. It is extremely powerful and quite useful for debugging backend code.
The only Cart event that I’ve ever managed to trigger with the above code is when a current cart is emptied by completing the order, and then I start another round of shopping.
I would love to see a working example of the shopping cart events being triggered as described in the API document.
Any updates about this? Doesn’t work for me either, and nothing shows up in Chrome’s Dev Console or in the Site Monitoring dashboard.
Tried a few events functions in both events.js and eventS.js:
import wixData from 'wix-data';
import wixStores from 'wix-stores-backend';
export function wixStores_onCartCreated(event) {
let total = event.totals.total;
wixData.insert('backendLog', {'title': total});
console.log("eventS.js total:", total);
}
export function wixStores_onCartAbandoned(event) {
let cartId = event.cartId;
let abandonTime = event.abandonTime;
let buyerEmail = event.buyerInfo.email;
let formattedTotal = event.totals.formattedTotal;
wixData.insert('backendLog', {'cartId': cartId});
console.log("eventS.js cartId:", cartId);
}