events.js onCartCreated not working

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;


the collection is called backendLog

I can make the insert code work if I hook off a button onClick(0 event in the page, but the API events do not seem to work

PLEASE HELP - I WANT TO KEEP SOME OF MY HAIR!!!

Have a read of this previous post
https://www.wix.com/corvid/forum/corvid-tips-and-updates/you-can-log-console-messages-in-your-backend-code

Also, see the Site Monitoring feature.

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 :slight_smile:

@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 :wink:

@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 :crossed_fingers::four_leaf_clover:

@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);
}

@yisrael-wix

Site monitoring doesn’t help, please see below…

Hi,
I am having the same issue. events.js is not triggering. Has anyone solved the issue? Thanks a lot for a feedback.

Hi, Wix Team,

Can you let me know if it is a bug or if we are doing something wrong? Thanks for some feedback…

Hey Thrishti; Please check the rules of the forum. Bumping a thread with “I have the same issues” isn’t allowed.Please make a new post and follow the post guidelines here: https://www.wix.com/corvid/forum/community-discussion/corvid-community-guidelines
It’s really helpful to get an appropriate answer. =)