event handler in events.js not triggered

The events.js (in Backend folder) is as below:

import wixData from 'wix-data';
import wixStores from 'wix-stores-backend';

export function wixStores_onCartCreated(event) {
  console.log("onCartCreated triggered");
  wixData.insert("Logs", {
    details: "onCartCreated triggered",
  });
}

export function wixStores_onCartAbandoned(event) {
  console.log("onCartAbandoned triggered");
  wixData.insert("Logs", {
    details: "onCartAbandoned triggered",
  });
}
  1. The “Logs” collection permission has been set to Anyone for view/create/update/delete

  2. I tested in live website. When I add an item to the cart or empty the cart, no entry shows up in the “Logs” collection and nothing in the Site Monitoring window.

  3. I have searched the web and this forum including the following link whose solution does not solve my case: https://www.wix.com/corvid/forum/community-discussion/how-setup-the-oncartcreated-events-wix-stores-backend?origin=auto_suggest

2 Likes

In the linked forum post, a working solution is given on that post that you can use.

Otherwise, what are you hoping to gain from the functions when you are not doing anything after you have called them?

Try at least doing what the code samples in the Wix API give you sample code for to check that it is working with just the samples.

Examples
An event when a shopping cart is created
Place this code in the events.js file of your site’s Backend section.

export function wixStores_onCartCreated(event) {
  let total = event.totals.total;
}

/*  Full event object:
 * {
 *    "cartId": "6d158831-1510-44ae-a420-1483a4200852",
 *    "creationTime": "2019-02-27T12:03:22.079Z",
 *    "buyerInfo": {
 *      "id": "4kf9ka09-4e9f-a02d-972f-9a5844d9d9a2",
 *      "email": "john.doe@somedomain.com",
 *      "phone": "5555555555",
 *      "firstName": "John",
 *      "lastName": "Doe"
 *    },
 *    "weightUnit": "LB",
 *    "currency": {
 *       "currency": "USD",
 *       "symbol": "$"
 *    },
 *    "totals": {
 *       "subtotal": 130,
 *       "discount": 0,
 *       "total": 130,
 *       "quantity": 1
 *    }
 * }
 */

Examples
An event when a shopping cart is abandoned
Place this code in the events.js file of your site’s Backend section.

export function wixStores_onCartAbandoned(event) {
  let cartId = event.cartId;
  let abandonTime = event.abandonTime;
  let buyerEmail = event.buyerInfo.email;
  let formattedTotal = event.totals.formattedTotal;
}

/*  Full event object:
 *  {
 *    "cartId": "60349781-b3hf-4349-8c9e-09492063da9",
 *    "creationTime": "2019-01-23T11:28:22.328Z",
 *    "abandonTime": "2019-01-23T12:28:02.373Z",
 *    "buyerInfo": {
 *      "id": "6g004532-732d-829f-5kf9-f9rk42afpla04m",
 *      "identityType": "MEMBER",
 *      "email": "john.doe@somedomain.com",
 *      "firstName": "John",
 *      "lastName": "Doe",
 *      "phone": "5555555555"
 *    },
 *    "itemsCount": 1,
 *    "couponId": "329a10d8-73a4-4a78-a68f-93a590c83672",
 *    "totals": {
 *      "subtotal": 20,
 *      "total": 20,
 *      "formattedTotal": "$20.00"
 *    },
 *    "checkoutUrl": "https://mysite.com/checkout?appSectionParams=%7B%22a11y%22%3Afalse%2C%22cartId%22%3A%2260819781-b3cf-4179-9c9e-4958kd89a9%22%2C%22storeUrl%22%3A%22https%3A%2F%2Fmysite.com%22%2C%22isFastFlow%22%3Afalse%2C%22isPickupFlow%22%3Afalse%7D&linkSource=acMail"
 *  }
 */

Also, make sure that you have set up your elements for Site Monitoring correctly.

https://support.wix.com/en/article/corvid-viewing-live-site-events
https://support.wix.com/en/article/corvid-generating-a-site-monitoring-event-to-debug-your-site

https://www.wix.com/corvid/reference/site-monitoring.html
https://www.wix.com/corvid/reference/site-monitoring.LogEntry.html