# WordPress.org

## Calculated Fields Form[resolved] Struggling with formula that requires if statement (19 posts)

1. winnard
Member
Posted 1 year ago #

Hi guys,

Great plugin. I am trying to figure out a solution to my current calculated form.

I have the current formula working in my form.

(fieldname3*fieldname10)+(fieldname1+fieldname6+fieldname8+fieldname12)

This works great. What I want to achieve is an if statement on the first part of the formula.

Fieldname3 is where user enters purchase price of a house property.
Fieldname10 is a dropdown set with the following options.

< £125,000 = 0%
> £125,000 < £250,000 = 1%
> £250,000 < £500,000 = 3%
> £500,000 < £1,000,000 = 4%
> £1,000,000 < £2,000,000 = 5%
> £2,000,000 = 7%

I would like to do a formula whereby I can remove these options from the user side and the calculation works when the user enters the house value in Fieldname3. Is this possible? If so could somebody help me out??

So it would be

if ((fieldname3 < £125,000) * 0.00) +(fieldname1+fieldname6+fieldname8+fieldname12) and so on to cover the other %.

If anybody could help me it would be most appreciated.

Thanks

Dan

2. codepeople
Member
Plugin Author

Posted 1 year ago #

Hi,

The equation in this case is not complex, please use the equation below:

(function(){
var v;
if(fieldname3 < 125000) v = 0;
if(fieldname3 >= 125000 && fieldname3 < 250000) v = 0.01;
if(fieldname3 >= 250000 && fieldname3 < 500000) v = 0.03;
if(fieldname3 >= 500000 && fieldname3 < 1000000) v = 0.04;
if(fieldname3 >= 1000000 && fieldname3 < 2000000) v = 0.05;
if(fieldname3 >= 2000000) v = 0.07;

return fieldname3*v+fieldname1+fieldname6+fieldname8+fieldname12;
})()

3. revitfactory
Member
Posted 11 months ago #

Hi I would also like to do a similar result but I need to display text instead of a numerical value. I cant seem to get it working with text. here is what I have:

``````(function(){
if(fieldname12 == 0) return "Bill MOnthly";
if(fieldname12 == 0.15) return "Bill Annually";

})();``````

Many thanks

4. codepeople
Member
Plugin Author

Posted 11 months ago #

Hi,

I'm sorry, but the Calculated Field requires a number as value. Remember, the calculated field is the result of a mathematical equation.

Best regards.

5. revitfactory
Member
Posted 11 months ago #

Ah, ok thanks.

Is there another way I could get text to change based on a dropdown choice?

Basically I'm trying to get the text under the total cost to say either 'Billed Annually' or 'Billed Monthly' based on their choice higher up the page when they choose discount options:
Monthly - 0% Discount.
Annually - 15% Discount.

6. bob1483
Member
Posted 11 months ago #

Hi revitfactory
you can have two different text fields and based on what you select in dropdown one of them will appear.
just click on "show dependencies" in dropdown settings and chose one of the text fields for each choices of dropdown.

7. revitfactory
Member
Posted 11 months ago #

excellent that works great for me. Thanks for a prompt response!

8. bob1483
Member
Posted 11 months ago #

Anytime buddy

9. revitfactory
Member
Posted 11 months ago #

Hi, sorry just one more problem. Im trying to add custom css which has worked fine in most scenarios but i cannot get the font height to change to bold?

I created a custom css as shown below but it doesn't work. Yet if i try changing the color: red; it works fine... even tried adding !important

``````.totalpayment {
font-style:bold !important;
}``````

10. revitfactory
Member
Posted 11 months ago #

Sokrry just realised I had the wrong code in the last post I have code shown below but it still doesnt work. Thanks

``````.totalpayment {
font-weight:bold !important;
}``````
11. bob1483
Member
Posted 11 months ago #

Actually you dont need css to bold the font. Just use html code in the field you want it to be bold.

12. revitfactory
Member
Posted 11 months ago #

Fantastic Thanks

13. energynerd
Member
Posted 9 months ago #

Is it possible to return a fixed value and a variable value from this formula:

(function(){
var v;
if(fieldname3 < 125000) v = 0;
if(fieldname3 >= 125000 && fieldname3 < 250000) v = 0.01;
if(fieldname3 >= 250000 && fieldname3 < 500000) v = 0.03;

return fieldname3*v+fieldname1+fieldname6+fieldname8+fieldname12;
})()

So if fieldname3 was < 125000 it could return a fixed fee i.e. return v rather than fieldname3*v?

14. codepeople
Member
Plugin Author

Posted 9 months ago #

In that case you only need to replace:

if(fieldname3 < 125000) v = 0;

by

if(fieldname3 < 125000) reutrn <fixed value>;

for example:

if(fieldname3 < 125000) return 100;

The other equation's parts don't require be modified:

(function(){
var v;
if(fieldname3 < 125000) return 100;
if(fieldname3 >= 125000 && fieldname3 < 250000) v = 0.01;
if(fieldname3 >= 250000 && fieldname3 < 500000) v = 0.03;

return fieldname3*v+fieldname1+fieldname6+fieldname8+fieldname12;
})()

15. energynerd
Member
Posted 9 months ago #

Thank you!

16. energynerd
Member
Posted 9 months ago #

Sorry to ask again, but what happens if there is another field, fieldname2. Fieldname2 and Fieldname3 are dependent and shown based on the value selected from a radio button so that one or both can be visible.

I tried something similar to the option below but the formula would always return 100/200 respectively plus there is no way for the formula to calculate if both fields are less than 125000.

(function(){
var v;
var w;
if(fieldname3 < 125000) return 100;
if(fieldname3 >= 125000 && fieldname3 < 250000) v = 0.01;
if(fieldname3 >= 250000 && fieldname3 < 500000) v = 0.03;
if(fieldname2 < 125000) return 200;
if(fieldname2 >= 125000 && fieldname3 < 250000) w = 0.04;
if(fieldname2 >= 250000 && fieldname3 < 500000) w = 0.06;

return prec(((fieldname2*w/100)+(fieldname3*v/100)),2);
})()

Is there a way to incorporate the formula to include an if radiobox value or similar?

The final part of my question is that if a user selects the radio button so both fields are visible, enters values into each field and then changes the radiobox value so that now only 1 field is visible, the formula still calculates the value previously entered into the now hidden field. I was hoping the if radiobox value field could handle this as well?

17. codepeople
Member
Plugin Author

Posted 9 months ago #

Hi,

Yes, the equation can be configured just like you want. I can't tell you exactly how because I don't know that you pretend calculate, but if you have, for example, the radiobutton fieldname4 with three possible options: 'value1', 'value2', 'value3', the final equation should be modified in the following way:

(function(){
var v;
var w;
if(fieldname3 < 125000) return 100;
if(fieldname3 >= 125000 && fieldname3 < 250000) v = 0.01;
if(fieldname3 >= 250000 && fieldname3 < 500000) v = 0.03;
if(fieldname2 < 125000) return 200;
if(fieldname2 >= 125000 && fieldname3 < 250000) w = 0.04;
if(fieldname2 >= 250000 && fieldname3 < 500000) w = 0.06;

if( fieldname4 == 'value1'){
// ... return the value corresponding
}
if( fieldname4 == 'value2'){
// ... return the value corresponding
}
if( fieldname4 == 'value3'){
// ... return the value corresponding
}
})()

If you hide a field and this field is part of the equation, its value will be used in the equation result.

18. energynerd
Member
Posted 9 months ago #

Thanks for that, nearly what I require. The return formula for one of the values is:

return prec(((fieldname3*v/100)+(fieldname2*w/100)),2);

However if both fiedname2 and fieldname 3 are < 125000, I need the formula to sum these two retruned fixed values?

19. codepeople
Member
Plugin Author

Posted 9 months ago #

Hi,

Please, follows the same structure of previous equations to check the values of fieldname2 and fieldname3 and return the correct value in that case.

This topic has been closed to new replies.