Session in Wix backend

I do not have login/signup in my website. However for each user i pass a custom string to backend/StorageModule.jsw when he presses add to cart button.

Later when the checkout is completed wixStores_onNewOrder function of backend/events.js is get called, but i do not have the custom string present there.

I tried an approach to insert custom string to a collection from backend/StorageModule.jsw and later access it in backend/events.js, but since i don’t have login/signup how will i differentiate between different users using the website simultaneously.

is there any type of session or any other approach available to solve this ?

I’m pretty sure the wix-storage module is front-end only since it is dealing with browser storage. If the string can be created in the backend, I would use the onCartCreated( ) event and insert a new item into a database where the _id field is equal to the buyerInfo id.

/*  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
 *    }
 * }
 */
 

Then use the onNewOrder() event and do a get call for your database with the buyerInfo id. You can also use this step to update the item in the database to also have the order number, user email, and maybe the amount if you are using the string later.

/*  Full event object:
 *  {
 *    "orderId": "2cd413bd-ddea-4101-b122-e8b146fec05f",
 *    "number": "10005",
 *    "dateCreated": "2018-08-05T12:33:18.938Z",
 *    "buyerInfo": {
 *      "id": "6g004532-732d-829f-5kf9-f9rk42afpla04m",
 *      "identityType": "MEMBER",
 *      "email": "john.doe@somedomain.com",
 *      "firstName": "John",
 *      "lastName": "Doe",
 *      "phone": "5555555555"
 *    },
 *    "currency": "USD",
 *    "weightUnit": "LB",
 *    "totals": {
 *      "subtotal": 99.99,
 *      "shipping": 4.99,
 *      "tax": 8.87,
 *      "discount": 9.99,
 *      "total": 103.86,
 *      "weight": 1.37,
 *      "quantity": 2
 *    },
 *    "paymentStatus": "PAID",
 *    "fulfillmentStatus": "FULFILLED"
 *  }
 */

They should have an ID if they are a visitor or a member. So it should work without people logging in.

If you can’t do the custom string in the backend, do the same when they hit add to cart except get the userId user the wix-users module. And if you want to the insert in the backend, send an object that has the user id and the string

example

let item = {
    "_id": wixUsers.currentUser.id,
    "string":string
}
backendInsert(item);

Hope this helps! If it does, mark it as top comment!

1 Like

Hi, I like the comment, but the wixUsers.currentUser.id does not equal the buyerInfoId. You have to some how connect the 2 on the back end with like a database? Is there a way to get that buyerinfo id from the current userid?