How would I be able to set up and input field that would act as a discount field? I already have the calculation part done, but need to figure out how get an input to act as a promo Code area.
top of page
Important forum update
This forum is migrating to one unified Wix community forum starting July 26th, and will be read-only during the process.
Wishlist Page is the official platform for requesting new features. You can vote, comment, and track the status of the requested features.
bottom of page
I changed the total to 15, but would like the total to equal what is in the DB
The ‘amount’ is the amount of the actual discount value I would like users to get off the final price
$w('#button64').onClick(event,$w)=>{ you missed the arcs '(' after onClick, you need to be more careful.
it should be done like this: $w('#button64').onClick((event,$w)=>{
also don't forget it to close it ')' at the end of the code.
the other things, try your codes step by step and make it bigger.
before writing the whole code, try to make simple actions to be sure that you start right.
try the button to print a word > console.log('pressed!')
finally what do you mean by this ? total = 'amount'
Thank you so much! I will try this out and let you know how it goes.
as a first step, think about the discount codes. You need to make a collection/table that has all the codes you have and the value of each one.
as a second step, you should be able to submit your form without trying to access the codes(the code input stills empty).
as, a third step, if the user wrote anything in the code input, you should check if this code existed in the database and make the discount if the discount code is active.
wixData.query("DiscountCodes") .eq('title', $w('#userInput').value) .eq('active',true) .find() .then( (results) => {
if(results.length!==0){ //make what should be done. }
else{ // make a notice that this code is not available. }
});\
the previous code should be inside the onClick method for your button.
$w("#submitButton").onClick( (event, $w) => { if( $w('#userInput').value == undefined){
// make what should be done.
}
else{
// the previous code. (checking the availability of the discount code in the collection.)
}
});
try these steps and I would help you more.
good luck!
@carlosgalvarez Could you provide an example? I would really appreciate it.
Do you have a link for your site, I'll recomend using .eq for the query. Your database will have the title promo code which can be like, "backToSchool and then another column with the discount, another one could be valid true/false. When you make the query It'll return the object containing the information where you can extracted from so you can tell the user that the code has expired, is not valid or the actual discount value.
When presenting it in text it must be a string and a number for operations.
This is the code I have. Everything works aside from the actual query. Any value entered in the input will show group8 and subtract 15 on total.
export function button64_click(event, $w) { wixData.query('DiscountCodes') // Query the collection for any items whose "Name" field contains // the value the user entered in the input element .contains('backToschool', $w('#input3').value) .find() // Run the query .then(res => { // Set the table data to be the results of the query $w('#group8').show(); total = 15; calc() } ); }
Can anyone help?
@carlosgalvarez this is the code I am using. I would like to (1) ensure the field is not required for users if they do not fill it out and (2) have multiple discount codes for multiple promotions. And if possible, explain how I would ensure certain promo codes are used once by a user (not essential, but would be great if you can explain).
$w.onReady(function () { $w("#input3").onCustomValidation( (value, reject) => { if (!value.includes("Promo Code 1")) { reject("Value must include Promo Code 1."); $w('#text227').show(); $w('#group8').hide(); total = 0 } else { $w('#text227').hide(); $w('#group8').show(); total = 15 } calc() } ); });
Can you be more specific on the workflow you want to accomplish, also how is your custom validation process
I have made a custom validation, but I can only use one discount code and if it is left blank its doesn’t let the user submit the form without entering the correct promo code