# WordPress.org

## Calculated Fields Form[resolved] sum if .. (6 posts)

1. oligou
Member
Posted 3 years ago #

Hello,

Is it possible to add a condition ?

THis condition will be an "IF", e.g. if the previous fields (list of 2 items) is A then do B+C, otherwise C+D

like in xl ...

Thank you

2. oligou
Member
Posted 3 years ago #

My apologies... plenty of topics resolving that

sorry, tx

3. oligou
Member
Posted 3 years ago #

I still have a question, how to put several if ?

E.G. a fieldnameY mention 1-2-3 or 4

If 1 : fieldnameX *80%
If 2 : FieldnameX *85%
If 3 : FieldnameX * 90%
If 4 : FieldnameX * 100 %

Any idea ?

THank you !

4. codepeople
Member
Plugin Author

Posted 3 years ago #

Hi,

The Calculated Field allows to insert complex equations using a javascript code as below:

(function(){
if(first condition) return first value;
if(second condition) return second value;
...
if(condition X) return value X;
})()

5. oligou
Member
Posted 3 years ago #

thank you

can you just put the first condition as example ?
I don't know this code

Thanks a lot :-)

6. codepeople
Member
Plugin Author

Posted 3 years ago #

Hi,

Please, I'll try to explain the code with an example:

Suppose your form contains the field "fieldname1" and you want add 10 to fieldname1 if its value is less than 10, if fieldname1 is less than 100 adds 100, and finally add 1000 to fieldname1 in other cases.

The equation that correspond to the previous situation is:

(function(){
if(fieldname1 < 10){
return fieldname1+10;
}else if(fieldname1 < 100){
return fieldname1+100;
}else{
return fieldname1+1000;
}
})()

This topic has been closed to new replies.