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 detail 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) ) }
screenshot showing where the if statements are placed.
This was my original screenshot when I first wrote this article.
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.
8 Comments
  • 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

  • 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 *