Unique Validation in specific User Input

I am working on an e-commerce website and I have created a cancellation request page wherein customers can fill in their order id and request for the cancellation of their orders. However, I want that a person shouldn’t be able to create similar requests for the same Order_ID i.e there should not be two requests for the same Order ID. Any help with Wix Code in this situation? I have included a screenshot of what the page looks like.

Hi!

What did you use as your order ID?
Is it a string that you generates or is it the default ID that every Item receives upon creation?

Please share a link to your site so one of us (Wix workers) can inspect it and provide you with a solution.

Best of luck!
Doron. :slight_smile:

www.rat-store.com/ca

I will use the invoice number as order ID. I am still working and have not yet decided it although. If possible give me the best option that you think will work good. P.S I am not a pro yet.

Hi Vipul!

There are two ways to solve this issue (probably more but I’ll mention two).
First: You could use a beforeInsert( ) DataHook, and upon submitting the cancelation form use a WixDataQuery to search the database if there was previous cancelation request with a matching order ID and reject the form if the result is greater than 0.
Second: Your other option (and my suggestion) is to create “my orders” page that will present the customer with his current orders and that the access to the cancelation form will be through the active orders.
Simply add a boolean field to the database (to the orders items in it) that is ‘true’ when a cancelation form was submitted for that particular order and grant access to the page only when it is ‘false’.

Hope it helps.
Best of luck!

Doron. :slight_smile:

1 Like

Hi Doron!

Thanks for your reply. But creating a My Orders page will probably require me to create a user profile for each customer. That isn’t supported by wix yet, I guess. Can you help me or maybe provide some links that might be useful in doing that? It will be quite helpful for me. :smiley:

Best,
Vipul.

Hi again!
I did not get this part:

“…“my orders” page that will present the customer with his current orders and that the access to the cancelation form will be through the active orders.
Simply add a boolean field to the database (to the orders items in it) that is ‘true’ when a cancelation form was submitted for that particular order and grant access to the page only when it is ‘false’.”

I cannot understand where are to asking to create the boolean field? I have created a dataset called “CancellationRequests” & that’s currently handling the form response. So do you want me to add the field there?

Ok so it seems wix now has the my orders page feature but I need a bit clarification on the second suggestion you gave. I am sharing with you a screenshot of my Dataset.

you can see that I was able to submit two requests with the same order_ID.

Hi Vipul!

You’ve asked about several topics so I’ll answer in order.

First, “Creating a My Orders page will probably require me to create a user profile for each customer”
While it is true that in order to associate orders to a certain user you need to create a membering system (including a database that will hold their records and a platform-page for them to use), you can simply use a Dynamic Page in order to automatically generate a page for each of your site users using a simple and constant format.
Keeping track after the site users might be very useful in the future for the site owner.
By that he (the owner) can either validate the identity of the users before making actions, provide them with a comfortable area for them to make actions on your site or just keep track after orders status.

As for the boolean field , (the access to the cancelation form was a suggestion given that you do use a system that tracks after your users) YES! The “CancellationRequests” database is the place to add it.
Giving every item (Cancellation request) in the database is associated with an unique order - when the submitting of a new cancelation request occurs, a code should be executed, that checks if:
1. The order exists.
2. If it does, is there a Cancelation request for it? (Thats where the boolean field used for -Is it currently set on “true”?)
If the order exists (wherever you store the tracking after the active orders) and there is no active cancelation request (“false”) - the form is submitted and the boolean variable should be turned to “true”.
If upon checking, the variable is set to “true” the form will be rejected (and the user should be getting an error message).
It naturally also solves the “how to avoid from creating multi-cancelation requests”
But yet again, this is just my own suggestion.

As for how to avoid from creating multi-cancelation requests/OrderID.
I didn’t manage to find the way that the value of ORDERID is generated in your site (please describe the path) but as for checking if cancelation request exists for a certain ID you can also do it by WixDataQuery and it would probably look something like that:

import wixData from 'wix-data';

export function button1_click(event, $w) {
    wixData.query("CancellationRequests")
    .eq("ORDERID", $w('#input2').value)
    .find()
    .then(res => {
          if (res.length > 0) {
              //show error message - existing cancelation form
          }
          else {
              $w('#dataset1').save()
              //form submitted - action succeeded
              .then(console.log("save done"))
          }
    })
}

I hope that I managed to answer all your question.
Best of luck!

Doron. :slight_smile:

Hi Doron,

I tried the code that you suggested but the form is still getting submitted even if the order id exists. What should happen is that the form should show a message that reads that “request exists” & the data should not submit into the dataset. As for the OrderID it is simply the number that WIX stores generates for each order that the user places. It is generated by the app itself I guess and is a number that looks like 10002. I am not acquainted with such kind of coding so it would be great if you could point me out some resources (about WIX code regarding this issue) that would help me with this topic or maybe provide a self explanatory code.

Best,
Vipul