Discount code fields

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.

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

Can you be more specific on the workflow you want to accomplish, also how is your custom validation process

@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 anyone help?

Why don’t you add the promo code text input and a validate button, when user clicks validate you make a query to a promo codes data base that includes the code and the type of promotion, then you present the information.

1 Like

@carlosgalvarez do you know how I could do that?

1 Like

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()
  } );
}
1 Like

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.

1 Like

https://brettfranklin2.wixsite.com/website-2/print-ad

1 Like

@carlosgalvarez Could you provide an example? I would really appreciate it.

1 Like

@carlosgalvarez any ideasfor this? Or anyone else? I really appreciate it as this is a very important feature on my website.

1 Like

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!

1 Like

Thank you so much! I will try this out and let you know how it goes.

1 Like

give it your best! Good luck :wink:

@ahmdjsalhi so I just tried it and I’m getting an error…

$w('#button64').onClick(event,$w)=>{

 if( $w('#input3').value == undefined){
 // make what should be done.
         }


else{
        wixData.query("DiscountCodes")
            .eq('title', $w('#input3').value)
            .eq('valid',true)

            .find()
            .then( (results) => {

 if(results.length!==0){
 //make what should be done.
            total = 'amount'
            calc()
            $w('#group9').hide();
            $w('#text227').hide();
            $w('#group8').show();
            }

 else{
 // make a notice that this code is not available.
            total = 0
            calc()
            $w('#group9').show();
            $w('#text227').show();
            $w('#group8').hide();
      }
 
});
 // the previous code. (checking the availability of the discount code in the collection.)
        }
});
}

$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’

The ‘amount’ is the amount of the actual discount value I would like users to get off the final price

I think you are using it wrong.
alse maybe you are not callig the function in the right way.

however, I want to see the user interface that you have and the code of cal() function please, to understand what you’ve done better.
also how you do you get the item price?