I can’t get the backend events to work at all.
I have created the file events.js in the Backend section. Here is its content:
import wixData from 'wix-data';
import wixStores from 'wix-stores-backend';
export function wixStores_onCartCreated(event) {
let total = event.totals.total;
console.log(event);
wixData.insert('Log', {'title': total})
}
export function wixStores_onFulfillmentUpdated(event) {
console.log(event);
wixData.insert('Log', {'title': JSON.stringify(event)});
}
I have also tried with
import wixStoresBackend from 'wix-stores-backend';
export function wixStoresBackend_onCartCreated(event) {
...
When I add an item to an empty cart or go into Orders admin section and change fulfillment status, nothing appears in the Real-Time Errors and Logs; nothing is inserted into the Log collection.
events.js and the Log collection are published. Log is set to ‘anyone’ permissions.
I am really at a loss as to why nothing is working.
I see many posts about similar problems, and ultimately none of them present a working solution (as per follow-up comments).
Ultimately, I want to process paid orders on another server by doing this:
import { fetch } from 'wix-fetch';
import wixStoresBackend from 'wix-stores-backend';
export function wixStoresBackend_onOrderPaid(event) {
send(event);
}
function send(data) {
console.log("Sending order data...");
console.log(data);
let url = "https://orders.example.com/new";
fetch(
url, {
method: 'POST',
headers: {
'Content-Type': 'text/plain; charset=utf-8',
'Authorization': 'Bearer xxx'
},
body: JSON.stringify(data)
}
);
}
But it is very discouraging to see that events won’t work even for basic things like adding an item to a cart…
OK, with this code I am able to get something in the log appearing every time I open a new Incognito window and add an item to the cart:
events.js:
import wixStores from 'wix-stores-backend';
export function wixStores_onCartCreated(event) {
console.log(event);
}
I think part of the reproducibility problem is that onCartCreated() does not do what it is described in the documentation to do: “An event that fires when a visitor adds a product to a shopping cart.”
Instead, Wix seems to do something behind the scenes to determine whether this is a new visitor or not, and then triggers this event for the first item only that this new visitor adds to the basket.
I then gradually built up the script to what I wanted:
import wixStores from 'wix-stores-backend';
import { fetch } from 'wix-fetch';
export function wixStores_onOrderPaid(event) {
send(event);
}
function send(data) {
console.log("Sending data...");
console.log(data);
let url = "https://orders.example.com/protected/new";
fetch(
url, {
method: 'POST',
headers: {
'Content-Type': 'text/plain; charset=utf-8',
'Authorization': 'Bearer xxx'
},
body: JSON.stringify(data)
}
);
}
This works and I am receiving the data on my other server.
There is another problem in that for some reason SKU is missing from the JSON.lineItems but that is a topic of its own.
Interesting what you say regarding onCartCreated(), since it also seems to me according to the name of the event, it would only trigger when the cart is first created.
I’m going to check with QA on this and see what they say.
noting in log when i create the cart (initiate an order and add product) . when i run the export in on preview mode i get null to log (this is ok because on preview event is empty)
import wixStores from ‘wix-stores-backend’ ; export function wixStores_onCartCreated ( event ) { console . log ( event );}
noting in log when i create the cart (initiate an order and add product) . when i run the export in on preview mode i get null to log (this is ok because on preview event is empty)