Had created a way to get the order details data when it was placed by a customer to be pushed to another database.
The code below worked well in April, but now for some reason it is not!!
Has something been changed? Or we need a way to fire something in events.js?
Please help.
import wixData from 'wix-data';
import wixStores from 'wix-stores';
import wixStoresBackend from 'wix-stores-backend';
export function wixStores_onNewOrder(event) {
let newOrderId = event.orderId;
let toInsert = {
"log": newOrderId
}
wixData.insert("OrderID",toInsert)
.then( (results) => {
let item = results; //see item below
} )
.catch( (err) => {
let errorMsg = err;
} );
}
<Resolved> I have been suffering and pulling my hairs with this not firing backend events issues since yesterday.
In my case, It was neither "eventS.js" nor "import wixStores from 'wix-stores-backend'" It was 'TEST SITE'!
You should "Publish" first to make backend event handlers in 'events.js'
Then, all changes in event handlers code will be reflected in "Test Site" as well.
To reproduce:
0. Install wix blog 1. Publish your site using "Publish Button" site comment out any event handler in 'events.js' like
// events.js // export function wixBlog_onPostCreated(event) { // console.log('new blog posted!',event) // }
2. Use "Create Test Site" to make test site with the event handler uncommented
//events.js export function wixBlog_onPostCreated(event) { console.log('new blog posted!',event) }
3. Open Developer Tools> Site Events>
4. Try add a blog post. You will see nothing in Site Events log. onPostCreated handler was not triggered.
5. Now, "Publish" site. And add a blog post. You will see a log in Site Events. onPostCreated handler triggered!
6. Now change code in events.js
//events.js export function wixBlog_onPostCreated(event) { console.log('new blog posted in Test Site',event) }
7. Check Event log. You will see a log 'new blog posted in Test Site' now triggered with new code.
I think events.js handlers are registered only when they are published. Once registered in backend, triggered with new code in events.js
I hope this saves you time.
export function wixBookings_onBookingConfirmed(event) { console.log(event) } export function wixBookings_onBookingRequestCreated(event) { console.log(event) }
I am just trying to perform some functions on wix booking events but there are no logs as well.. not sure why.
Have you simply just tried using the code from the example to see if that works for you, plus as Shan mentions above that you don't need to add the import for Wix Stores either.
https://www.wix.com/corvid/reference/wix-stores-backend.Events.html#onCartCreated
Also, note that if you test it just on preview mode that it won't work for you.
Note
Backend events don't work when previewing your site.
Finally take note of the code examples here from Adas as you have an extra semi colon on your insert line.
https://www.wix.com/corvid/forum/community-discussion/stores-order-hooks-not-working
Tested it with this code in a published site:
import wixData from 'wix-data'; import wixStores from 'wix-stores-backend'; export function wixStores_onNewOrder(event) { let newOrderId = event.orderId; wixData.insert('Logs', {'orders': newOrderId}) }
`orders` is a reference field in Logs collection, so it shows me the the order from Stores/Orders collection.
import wixData from 'wix-data'; import wixStores from 'wix-stores-backend'; export function wixStores_onCartCreated(event) { let total = event.totals.total; wixData.insert('Logs', {'title': total}) } export function wixStores_onNewOrder(event) { let newOrderId = event.orderId; wixData.insert('Logs', {'orders': newOrderId}) }
Hello, I'm stuck on the same issue. The adding to the cart is being done using the full page instead of the quickAdd window. But still, the event is not being fired. here is the code for events.js file: I want to make an api call after the purchase is completed, for testing purposes first, I tried to simply log the event and add it to backendLog collection as well because some other posts suggested to do so to see if its even firing. Its not. Anything I'm missing here ?
import { fetch } from "wix-fetch"; import wixData from 'wix-data'; // import wixStores from 'wix-stores'; import wixStores from 'wix-stores-backend'; export function wixStores_onCartCreated(event) { let total = event.totals.total; console.log("cart event fired"); wixData.insert('backendLog', {'title': total}); }
FYI you do not need to import the Wix Stores API on the events.js file. It will work without it.
Also, if you are using the onCartCreated function note that it will not fire when a user adds an item to the cart from the quick view window like below
Well it worked now!! I had to change import wixStoresBackend from 'wix-stores-backend';
TO
import wixStores from 'wix-stores-backend'; This surely has to be some sort of bug or the API library needs to be updated?! https://www.wix.com/corvid/reference/wix-stores-backend.Events.html
Hi,
As I wrote you before try to export again the event from the property panel .
And make sure this event is been written in a backend file.
https://www.wix.com/corvid/reference/wix-stores-backend.Events.html#onCartCreated
ok, if you put console.log() inside the export function can you see it in the browser? if not try to export again the function, maybe you changed the id name of the element.
Best,
Sapir
Don't have a picture of it since there is no error. The backend just doesn't fire. It was previously. I tried looking at the console but nothing there too.
Hi,
Can you send a picture of the error you get?