Events.js previously firing, now it is not

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

Hi,

Can you send a picture of the error you get?

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.

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

This is what I have now and I still donā€™t see the log in the console. The function in events.js is not firing.

This is also on a unpaid wix site for testing purposes. Does that make a difference? I hope so not.


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

export function wixStores_onCartCreated(event) {
 let total = event.totals.total;
  console.log("cart event fired");
}

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

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

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

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

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})
}
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.

I have exactly the same problem. My wixStores_onNewOrder() is not fired. What do you mean with ā€œtry to export again the event from the property panelā€? This is my 1st day with wix and somehow I am lost.

I have this post https://www.wix.com/velo/forum/coding-with-velo/wixstores-onneworder-not-fired - can you please help. I have no clue why the url is not called.

Thank you!

@office72724
Here comes the PROPERTY-PANELā€¦

@russian-dima

Itā€™s not accessible - I am in ā€œCode Filesā€ | ā€œBackendā€ | ā€œevents.jsā€.
When I click in ā€œPage Codeā€ and there for example on ā€œmasterPage.jsā€, yes, then itā€™s enabled.

What can I do?

OK guys, I could solve it. This was my 1st day with WIX. The site is not using wix Store - so of course, no event could be fired. May be this comment helps somebody else.

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)
// }
  1. 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)
}

  1. Open Developer Tools> Site Events>

  2. Try add a blog post. You will see nothing in Site Events log. onPostCreated handler was not triggered.

  3. Now, ā€œPublishā€ site. And add a blog post. You will see a log in Site Events. onPostCreated handler triggered!

  4. Now change code in events.js

//events.js

export function wixBlog_onPostCreated(event) {
  console.log('new blog posted in Test Site',event)
}
  1. 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.

Hi,
unfortunately iā€™ve tried EVERYTHING in this thread and failed.
im simply trying to get a single console.log to see if i can trigger a backend event.
my main goal is to add an order to ā€œordersā€ collection (iā€™ve created it manually with extra columns i need) when the order is paid.

but im so much behind it.

I canā€™t see any backend event logs.
I cant open properties panel on ā€œevents.jsā€
im using new eCom api.

im frustrated of all the trials to solve it.

please assist.

here is my events.js panel:

here is the current Code:

import wixData from 'wix-data';

console.log('in the backend events.js');

export function wixEvents_onEventStarted(event) { 
    console.log('Cart created', JSON.stringify(event));
}

export function wixEcom_onCartCreated(event) {

  const cartSubtotal = event.entity.subtotal.amount;

  const cartId = event.entity._id;

  const eventId = event.metadata.id;

  console.log('Cart created', event)

}



export function wixEcom_onCheckoutCompleted(event) {

  const checkoutSubtotal = event.entity.subtotal.amount;

  const checkoutId = event.entity._id;

  const checkoutComplete = event.entity.completed;

  const eventId = event.metadata.id;

  console.log('Checkout completed', event)

}

export function wixEcom_onOrderUpdated(event) {

  const orderId = event.data.order._id;

  const orderTotalPrice = event.data.order.priceSummary.totalPrice.amount;

  const eventId = event.metadata.id;

  console.log('Order updated', event)

}

export function wixEcom_onCheckoutCreated(event) {
    console.log('Checkout created', event)
  const checkoutSubtotal = event.entity.subtotal.amount;
  const checkoutId = event.entity._id;
  const eventId = event.metadata.id;
  
}


export function wixEcom_onOrderCreated(event) {
    console.log("wixEcom_onOrderCreated")
    const eventId = event.metadata.id
    const entityId = event.entity._id;

}

export async function wixEcom_onOrderPaymentStatusUpdated(event) {
    console.log("event", JSON.stringify(event));
    const order = event.data.order;
    const orderId = order._id;
    const email = order.buyerInfo.email;
    const selectedTemplatesUrls = [];
    order.lineItems.forEach(item => {
        selectedTemplatesUrls.push({
            id: item._id,
            url: item.image
        })
    });
    const phone = order.buyerInfo.phone;

    console.log("orderId", orderId)
    console.log("email", email)
    console.log("selectedTemplatesUrls": )
    selectedTemplatesUrls.forEach(item => {
        console.log(JSON.stringify(item))

    });

    console.log('Order payment status updated', event)

    try {
        // Use a unique identifier for your order records, separate from Wix's _id
        let existingOrder = await wixData.query("orders").eq("orderId", orderId).find();

        if (existingOrder.totalCount === 0) {
            const newOrder = {
                email,
                phone,
                selectedTemplatesUrls
            };

            const createdOrder = await wixData.insert("orders", newOrder);
            console.log('Order inserted in database:', createdOrder);
        } else {
            console.log('Order already exists in database');
        }
    } catch (error) {
        console.error('Error updating order in database:', error);
    }
}


truly desperate hereā€¦ please assist.

Please open a new topic/post instead of bumping up very old posts, even if it is somehow related to your own post.

You can connect this post with your own.

1 Like

I was having the same issue then i realised i need to have the logs open in another tab whilst i test things on the live site in a seperate tab. Sometimes I just need to publish and refresh the live site if the logs are not showing. hope it helps