• Resolved landomat

    (@landomat)


    Hey Codepeople! I need your help with my calculation:

    We are selling tables online and for that i need the right formular.

    For Example:

    Field1: SUM
    Field2: A
    Field3: B

    Price / cm^2 = 0.05
    Tax: 1.2
    Discount1: 0.78
    Discount2: 0.76
    Discount4: 0.74
    Discount5: 0.72

    Basic Calculation:

    A * B * 0.05

    For bigger table formats, our customers get a discount:
    For this i need something like that:

    (function(){
    if(calculatedfield(SUM) >= 750) return A * B * 0.05 * 1.2 * 0.78;
    if(calculatedfield(SUM) >= 800) return A * B * 0.05 * 1.2 * 0.76;
    if(calculatedfield(SUM) >= 950) return A * B * 0.05 * 1.2 * 0.74;
    if(calculatedfield(SUM) >= 1000) return A * B * 0.05 * 1.2 * 0.72;
    })();

    Hope you can help me πŸ™‚

    https://wordpress.org/plugins/calculated-fields-form/

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author codepeople

    (@codepeople)

    Hi,

    In your equation is only needed replace the seudo-names, by the real names of fields in your form, and very important, invert the order of the conditional statements, because the first condition will be always true, even for the rest of conditions, if calculatedfied(SUM) is bigger than 800, is because is bigger than 750,

    So, I will call the fields with the names used by our plugin.

    SUM: fieldname1
    A: fieldname2
    B: fieldanme3

    and the equation would be:

    (function(){
    if(fieldname1 >= 1000) return fieldname2 * fieldname3 * 0.05 * 1.2 * 0.72;
    if(fieldname1 >= 950) return fieldname2 * fieldname3 * 0.05 * 1.2 * 0.74;
    if(fieldname1 >= 800) return fieldname2 * fieldname3 * 0.05 * 1.2 * 0.76;
    if(fieldname1 >= 750) return fieldname2 * fieldname3 * 0.05 * 1.2 * 0.78;
    return fieldname2 * fieldname3 * 0.05 * 1.2;
    })()

    Note1: Use the name of fields that correspond to your form.

    If you need an optimized equation, with less replacements, you can use:

    (function(){
    var s = fieldname1;
    var m = fieldname2 * fieldname3 * 0.05 * 1.2;

    if(s >= 1000) return m * 0.72;
    if(s >= 950) return m * 0.74;
    if(s >= 800) return m * 0.76;
    if(s >= 750) return m * 0.78;
    return m;
    })()

    Note2: My equations are always returning a value that is not controled by a conditional statements, because your original equation don’t return any value when is not valid a condition for discounts.

    Best regards.

    Thread Starter landomat

    (@landomat)

    Hello πŸ™‚ Thx for your fast reply!

    The second function is really nice πŸ™‚
    I copied the second function, but the plugin do not accept it :O

    Plugin Author codepeople

    (@codepeople)

    Hi,

    Could you send me the link to your page, to check if the equation has been included correctly, please?

    Best regards.

    Thread Starter landomat

    (@landomat)

    Hey Codepeople πŸ™‚ I got it!
    Works fine πŸ˜€

    But one more question:

    How to round it to 2 decimals… I used:

    (function(){
    var s = prec((fieldname1),2);
    var m = prec((fieldname2 * fieldname3 * 0.05 * 1.2),2);
    
    if(s >= 1000) return m * 0.72;
    if(s >= 950) return m * 0.74;
    if(s >= 800) return m * 0.76;
    if(s >= 750) return m * 0.78;
    return m;
    })()

    greetings

    Plugin Author codepeople

    (@codepeople)

    Hi,

    You simply should use the PREC operation, whose structure is PREC(x,y), where “x” is the number to modify, and “y” is the number of decimals. So, the equation would be:

    (function(){
    var s = prec((fieldname1),2);
    var m = prec((fieldname2 * fieldname3 * 0.05 * 1.2),2);
    
    if(s >= 1000) return PREC(m * 0.72,2);
    if(s >= 950) return PREC(m * 0.74,2);
    if(s >= 800) return PREC(m * 0.76,2);
    if(s >= 750) return PREC(m * 0.78,2);
    return PREC(m,2);
    })()

    Best regards.

    Plugin Author codepeople

    (@codepeople)

    Hi,

    I’m sorry, in this case the PREC operations in the “var” section are not needed:

    (function(){
    var s = fieldname1;
    var m = fieldname2 * fieldname3 * 0.05 * 1.2;
    
    if(s >= 1000) return PREC(m * 0.72,2);
    if(s >= 950) return PREC(m * 0.74,2);
    if(s >= 800) return PREC(m * 0.76,2);
    if(s >= 750) return PREC(m * 0.78,2);
    return PREC(m,2);
    })()

    Hi! It seems this plugin might be the one I am looking for.

    Just a few question as I got similar calculation scheme as above:

    1. So, I can use if condition as above ?

    2. Plugin do have a round function.

    3 Important and will be using with if condition for my project:
    Can I do a datepicker like Ex.

    Buying Date: 6/7/09 Selling Date: 5/7/15 (or today)

    if (sellingdate – buyingdate) < 5 years
    take this value —> v1;
    else take v2

    Plugin Author codepeople

    (@codepeople)

    Hi,

    I will answer all your questions by separated because would be easier to understand:

    Q: So, I can use if condition as above ?

    A: Yes, of course there are different ways to use conditional statements.

    Using the “IF” operation included by the plugin:

    IF(condition, value if condition is true, value if condition is false)

    Ternary operator:

    (condition) ? value if condition is true : value if condition is false

    Conditional statement:

    if(condition)
    {
    statements if condition is true;
    }
    else
    {
    statements if condition is true;
    }

    Important Note: JavaScript is a case sensitive language.

    Q: Plugin do have a round function.

    A: Yes, the operation ROUND

    ROUND(3.4) returns 3
    ROUND(3.7) returns 4

    Q:Important and will be using with if condition for my project:
    Can I do a datepicker like Ex.

    Buying Date: 6/7/09 Selling Date: 5/7/15 (or today)

    if (sellingdate – buyingdate) < 5 years
    take this value —> v1;
    else take v2

    A: I will use the structure of the fields names in our plugin:

    Buying Date: fieldname1
    Selling Date: fieldname2
    v1: fieldname3
    v2: fieldname4

    The difference between two dates returns the number of days between them, you simply should modify the operation like follows to get the number of years:

    ABS(fieldname2-fieldname1)/365

    So, using the different solutions, explained in the first question:

    Equation 1:

    IF( ABS(fieldname2-fieldname1)/365 < 5, fieldname3, fieldname4 )

    Equation 2:

    (ABS(fieldname2-fieldname1)/365 < 5) ? fieldname3 : fieldname4

    Equation 3:

    (function(){
    if(ABS(fieldname2-fieldname1)/365 < 5)
    {
        return fieldname3;
    }
    else
    {
        return fieldname4;
    }
    })()

    Best regards.

    Awesome! Prompt and fast reply!!!!

    cheers,

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Discount Price Formular.’ is closed to new replies.