Formidable Forms is my favorite form creation plugin for WordPress.  It’s extremely flexible, and ideal for web developers.

The Situation

Recently I was asked to make a complex price calculator for a carpet cleaning company.  The pricing worked as follows…

1 room cost $79
2 rooms cost $109
3 rooms cost $134
4 rooms cost $159
5 rooms cost $184

What We’re Making

Before launching into the details let’s take a look at what we’ll be making…

If Statement Calculator Demo
$

Try changing the room number and see the result.

Why If Statements?

Since the pricing didn’t follow a predictable formula I needed to use if statements

The Formidable Field Calculations Documentation doesn’t mention anything about using if statements, but after looking at other the examples I  decided to do some experimentation…

if ( [carpet_rooms] == 1  ) { 79 } 

The above code works.  It basically says, if the rooms field is set to 1 the total will be set to 79.  It’s simple but  proves that if statements are possible.

By chaining if statements together, we can calculate the required pricing structure.  Here’s what it looks like.

if ( [carpet_rooms] == 1  ) { 79 } else if ( [carpet_rooms] == 2 ) { 109 } else if ( [carpet_rooms] >= 3 ) { 109 + (25 * ([carpet_rooms] - 2) ) }
An example of using and If Statement in Formidable Forms Calculations.
Here’s an updated screenshot of where to put the “If” statement in a Formidable Forms calculation.
17 Comments
  • Jeff LaBonte says:

    I can’t thank you enough for sharing this. It worked perfectly for a similar pricing challenge I recently encountered with a school’s summer class pricing (no consistent pattern for discounts). Formidable Support’s solution struck me as worse than the original problem. Your solution here is perfect.

  • Thanks For sharing That Code
    But I have some doubts when i did same that code but its won’t work for else if statement its work only if statement or else statement not else if
    here is code
    if ([78] <= "4.5" || [77] <= "3.5" || [79] <= "4.0" || [80] <= "4.0") { 4 } else if ([78] <= "5.0" || [77] <= "4.0" || [79] <= "5.0" || [80] <= "5.0") { 5 } else if ([78] <= "5.5" || [77] <= "5.0" || [79] <= "5.5" || [80] <= "5.5"){6} else {10}
    its result only 4 or 10 not 5 or 6

  • Jashandeep Singh says:

    I really appreciate that code I have some doubts my code doesn’t work for (else if statement ) it only works for if statement So please help to resolve this problem or give me some solutions.
    Here is code -> if ([78] <= "4.5" || [77] <= "3.5" || [79] <= "4.0" || [80] <= "4.0"){ 4 } else if ([78] <= "5.0" || [77] <= "4.0" || [79] <= "5.0" || [80] <= "5.0"){ 5 }
    if i put all value which is given on second codnition then its result is 0 instead of 5

  • Das says:

    Thank you for sharing this. It’s a lifesaver!

  • Ben says:

    Great – this fills a hole in the official documentation. Thanks for posting. For other folk I had to evaluate 2 fields to set a discounted price and used the && operator as follows (where the numbers in square brackets are the field ID’s):

    if ([279] == 260 && [278] == 1) { 245 } else if ([279] == 260 && [278] == 0) { 260 }

    • Yves says:

      Thank you very much for sharing this! I did exactly the same thing in a plug-in called Calculated Field Form. I am happy to see this is possible with Formidableforms too since I want to switch.

  • Jon says:

    nice one! works for me and a good addition to the doco!

  • Steven says:

    You code doesn’t work for 1 or 2 rooms, do you have solution for that?

    • Administrator says:

      It has always been working for me, but I just realized that I’ve been using Firefox (where it worked flawlessly) but I was surprised to discover it wasn’t work in Chrome. Thanks for pointing that out! I must say, I am surprised I didn’t see that earlier.

      In any case, it appears the fix is to use “else if” statements instead of just “if” statements.

      I have updated to code above, accordingly.

  • Steve Bragg says:

    Hi There

    I am trying to make this work with testing fields with text strings in them and have done this for example:
    if ( [1918] == “Farnham District” ) { [1956] } if ( [1918] == “Outside Farnham Scouts/Guides” ) { [1990] }

    Have you any experience of using if statements with text strings? I as ask the statement above does not work and only returns a zero all the time.

    Thanks

    • Administrator says:

      I did some testing, but I was unable to find a way to do that. Sorry.

    • Dario says:

      Hi Steve did you ever find a solution for this? I’m banging my head on the wall to find a solution to this:

      if ( [328] == “Service Item” ) { “POS” } else if ( [328] == “Non-Stock Item” ) { “POS” } else if ( [328] == “Stock item” ) { “Storage” }

    • Stephon Duncan says:

      The solution for this is to use a dropdown or radio option for your text values, then use the separate values option.. If the dropdown option is Farnham District then the seperate value will be 1.. From there you can do if ( [1918] == “1” ) { [1956] }

  • Harsh says:

    Hi
    Your guide really helped me a lot. Dont know why they didnt included in documentation. Thanks

  • HF says:

    WOW – Thank you – really helpful

  • Mh Shamim says:

    Is any line need with this following code? If you embed any video tutorial about this, that will be more help for me. However, an awesome information. Thanks

    • Administrator says:

      Yes that’s a good point. I actually started to make a video about this but got busy before having the chance to finish it. I’ll try to wrap it up and post it here in the near future.

Leave a Reply

Your email address will not be published. Required fields are marked *