Help with a calculation from multiple selections in a category field

Comments

11 comments

  • Automations Bot

    Hey, just use this sample code and replace the sample values with your app field value tokens to get this done:

    For example you have 3 products in that category field namely product A, B, C with each priced at $10, $20 and $30


    function calculateTotal(){
    let productNames = {"A":10, "B":20, "C":30}; //replace this object with your category fields and the price here
     
    let currentCategoryFieldValues = @ADD your category field token here;
    let total = 0;
    Object.keys(productNames).forEach((key)=>{
    if(currentCategoryFieldValues.includes(key)){
    console.log(`Matched key = `, productNames[key]);
    total += productNames[key];
    }
    })
     
    return "$"total;
    }
     
    calculateTotal()
    So when product A & C is selected in your category field, system will output $40 as the calculated price
     
    Let me know if you have any questions, you can email me at developer@automationexpert.in for any queries!
     
    Thanks!
    0
    Comment actions Permalink
  • Automations Bot

    Please don't put console.log line in the code

    Also I forgot to add base price to the total, so in the line where we return "$"total; instead do this: return "$"+total+@your base price field token here

     

    so this is the final code:

     

    function calculateTotal(){
    let productNames = {"A":10, "B":20, "C":30}; //replace this object with your category fields and the price here
     
    let currentCategoryFieldValues = @ADD your category field token here;
    let total = 0;
    Object.keys(productNames).forEach((key)=>{
    if(currentCategoryFieldValues.includes(key)){
    total += productNames[key];
    }
    })
     
    return "$"+total+@ADD your base price field token here;
    }
     
    calculateTotal()

     

    0
    Comment actions Permalink
  • Andy Barton

    Thank you Automations Bot I will give it a go.. 

    0
    Comment actions Permalink
  • Andy Barton

    Hi,

    I am getting an Unexpected Identifier error... The Code I have written is as follows:- I have also included a screen shot of the category field for your reference.. 

    function calculateTotal() {
        let productNames = {
           "Sewn on Tape":1.50, 
           "Length Changes":1.05, 
           "Coloured Collar":1.60, 
           "Special Knit Collar":1.30, 
           "Minimum Order Surcharge up to 6":50.00, 
           "Minimum Order Surcharge 7 to 11":25.00, 
           "Velbro Stripe or Panel":2.50, 
           "Zip":2,
           "Piping":2.50, 
           "Pockets with Trim":2.00, 
           :Pocket ":3.00}; //

                let currentCategoryFieldValues = @Garment Extras;
           let total = 0;
           Object.keys(productNames).forEach((key)=>{
                if(currentCategoryFieldValues.includes(key)){
           total += productNames[key];
                }
           })

           return "$" + total + @Base Price;
       }

       calculateTotal()

     

    0
    Comment actions Permalink
  • Automations Bot

    Hello Andy Barton due to the limitations of native javascript functions you're facing this issue, please create a new calculation field and add below code, also make sure you replace Base Price field with your base price field and Garment field with category field and I'm sure this will work, I have created a similar environment in my Podio and I have attached a screenshot below of the calculated price within that calculation field and Podio gives me correct price for the selected products!

     

    Code:

     

    calculateTotal();


    function calculateTotal() {

        var productArray = [
            "Sewn on Tape",
            "Length Changes",
            "Coloured Collar",
            "Special Knit Collar",
            "Minimum Order Surcharge up to 6",
            "Minimum Order Surcharge 7 to 11",
            "Velbro Stripe or Panel",
            "Zip",
            "Piping",
            "Pockets with Trim",
            "Pocket"
        ];

        var productPrices = [
            1.50,
            1.05,
            1.60,
            1.30,
            50.00,
            25.00,
            2.50,
            2.00,
            2.50,
            2.00,
            3.00
        ];

        //make sure the entry in productArray is directly relative to the productPrices array, that is Sewn on Tape cost is 1.50 so it is the first entry on both arrays and so on..

        var currentCategoryFieldValues = @Garment Extras ? @Garment Extras.toString().split(",") : "";
        var categoryFieldLength = currentCategoryFieldValues ? currentCategoryFieldValues.length : 0;
        var total = 0.0;

        for (var i = 0; i < productArray.length; i++) {
            for(var j=0; j<categoryFieldLength ; j++){
                if(currentCategoryFieldValues[j] == productArray[i]){
                    total = total+productPrices[i];
                }
            }

        } 
        var basePrice = @Base Price.toString()
        var returningResult = parseFloat(total) + parseFloat(basePrice.replace(/[^0-9]/, ""))

        return "$" + returningResult;
    }

     

    Let me know if you face any issues!

    0
    Comment actions Permalink
  • Automations Bot

    0
    Comment actions Permalink
  • Automations Bot

    This is the screenshot from my calculated field so it'll be more clear to you of what tokens you have to replace

    0
    Comment actions Permalink
  • Rainer Grabowski

    Hi Andy, 

    the code provided by Automations Bot can't work in a Podio calculation field. That code uses elements (like the keyword let or the arrow function =>) that have been introduced with the JavaScript version ES6 - but Podio uses the older version ES5 and doesn't allow any ES6 elements (which cause the error msg).

    Also you've a typo in the object productNames, last line: the colon before Pocket must be a quotation mark and the blank space behind Pocket must be removed.

    I think this code should work for you:

    var extras = @Garment Extras;
    var total = @Base Price || 0;
    if(extras != null){
     var extraPrices = {
           "Sewn on Tape":1.50, 
           "Length Changes":1.05, 
           "Coloured Collar":1.60, 
           "Special Knit Collar":1.30, 
           "Minimum Order Surcharge up to 6":50.00, 
           "Minimum Order Surcharge 7 to 11":25.00, 
           "Velbro Stripe or Panel":2.50, 
           "Zip":2,
           "Piping":2.50, 
           "Pockets with Trim":2.00, 
         "Pocket":3.00
         };
        for(var i = 0; i < extras.length; i++){
          total += Number(extraPrices[extras[i]]) || 0;
      }
    }
    "$" + total 

    Please note:
    A) Adding $ to the total makes this field a text type field. Means: If you need the total for further calculations in that following calculation you must remove the $ and parse that text (e.g. $110.00) to a number. 

    B) Product names and prices are hard coded. So if you change the category field (e.g taking one out or adding another extra) you must change your code too. Also if you change a price for an extra. Besides the fact that you can forget to change the code the important caveat of this hard coded version is, that a change in the code would change the results in all Quotes items - so all previously created quotes will get a new result (with the new prices) and aren't valid anymore. 

    The solution for B)  is an additional App called "Extras" with a single line text field for the product name and a number field for the price. That price you can change whenever you want and you can add new extras and rename or remove existing extras. But it needs Podio Workflow Automation (aka PWA or Globiflow). If you already use PWA or have a Podio Premium Account (which includes PWA) let me know and I'll describe this solution.

    Do you use the app Quotes as a web form whrere the users can make their selections?

    Rainer 

    0
    Comment actions Permalink
  • Andy Barton

    Thank you Automations Bot for your assistance. This works, but it is giving me a result of $4974 when it should be $49.74

    I replaced - (/[^0-9]/, "")

    with - (/\B(?=(\d{3})+(?!\d))/g, “,”)

    And I got the correct format $49.74

    Many Thanks again 

    Andy

    0
    Comment actions Permalink
  • Andy Barton

    Hi Rainer Grabowski

    Thank you for your help here, you really are a legend in the Podio Coding world.  Your code worked perfectly.  And thank you for your insight into a better solution for this problem. 

    Yes I am a Podio Premium subscriber and already use PWA.  

    The Quotes app is used by my sales staff to build prices on garments they are quoting to customers.  We have not set up the webform side of things but maybe something we think about down the track on our website. The reason we want it to be calculated this way with set values is so everyone's (sales staff's) Pricing is worked out the same across the board. So having one single app that we can change the price once, to affect the quotes in the system would be very helpful.

    I look forward to hearing your PWA solution, and thank you in advance.

    Have a wonderful day.  All the best 

    Andy

    0
    Comment actions Permalink
  • Automations Bot

    Rainer Grabowski, that's a very elegant solution!

    0
    Comment actions Permalink

Please sign in to leave a comment.

Powered by Zendesk