PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Dynamic Price Per Square-Metre Calculation

  1. mrhoneyfoot
    Member

    My Goal is to allow users to price-up a selected product by entering width and height measurements in millimeters to generate a price. Essentially to calculate price per square metre. Is this possible with gravity forms? Or with PHPurchase and gravity forms? Or has anyone heard of a solution out there?

    Cheers Guys.

    Posted 13 years ago on Thursday January 20, 2011 | Permalink
  2. Ok... so I got a little carried away here... and I have to throw out the disclaimer that we can't really offer this level of support on every forum request, but here is a really solid start on this. You piqued my interest. :P

    http://pastie.org/1481851

    There are five pieces of this you'll need to update for your form (there are notes in the code itself):

    1. The ID specified by the hook
    2. Height Input
    3. Width Input
    4. Price Input
    5. Price Per Meter

    I used number fields for the height and width and a price field set to the "user defined price" option for the price field. Hope this helps and don't tell my bosses. :)

    Posted 13 years ago on Thursday January 20, 2011 | Permalink
  3. mrhoneyfoot
    Member

    David Smith, I salute you. Really appreciate it mate. Works beautifully. Quick question though.

    A problem with a user defined field is that the user can type over it. Tried hiding the price field with conditional logic, hoping Total Price would step into the breach. However Total Price doesn't pick up the price field when it's hidden. David is there a way to get Total Price to pick up the hidden field? Given that Total Price works through javascript too, is it possible to adapt your snippet?

    Posted 13 years ago on Friday January 21, 2011 | Permalink
  4. mrhoneyfoot
    Member

    Think we might have found what's causing total price to remain at zero.

    Specifically, function gformGetBasePrice(formId, productFieldId){ has two almost identical instances of the following inside price.js:

    //If field is hidden by conditional logic, don't count it for the total
            if(gformIsHidden(productField)){
                price = 0;
            }

    and

    //If field is hidden by conditional logic, don't count it for the total
            if(gformIsHidden(productField))
                price = 0;
    
        }

    Should price = 0; be changed to price = productField.val(); Will that work?

    I know hacking price.js probably isn't the best solution due to upgrades. Would anyone know if this function can be overridden through functions.php?

    Posted 13 years ago on Saturday January 22, 2011 | Permalink
  5. mrhoneyfoot
    Member

    Well that didn't work. Two more functions might be causing this. Either this one:

    function gformIsHidden(element){
        return element.parents('.gfield').css("display") == "none";
    }

    or this part of the function gformCalculateProductPrice(formId, productFieldId){

    if(!gformIsHidden(jQuery(this)))
                price += gformGetPrice(jQuery(this).val());
        });

    Alternatively price = productField.val(); was not the answer. Anyone want to make a call?

    Posted 13 years ago on Saturday January 22, 2011 | Permalink
  6. The total field is a "just for show" field. You can manipulate the total field with javascript all you want, but the submitted value is not used to determine the "real" total of the submission. That is why we use the product field set to user defined price. The value submitted through this field does impact the "real" total of the submission. So, with that said, here is an updated snippet that still uses the user defined price, but hides it with a little CSS, but also uses the total field to provide that information to the user.

    http://pastie.org/1487231

    Posted 13 years ago on Saturday January 22, 2011 | Permalink
  7. mrhoneyfoot
    Member

    WORKS..! Thanks David Smith. You made my day. Much appreciated.

    Posted 13 years ago on Saturday January 22, 2011 | Permalink
  8. Perfect! This is exactly what I need, but I'm not sure what file this code needs to be added to. I know this is outside the realm of "standard" support, but this is incredibly helpful! Thanks.

    Posted 13 years ago on Wednesday March 2, 2011 | Permalink
  9. Hi comparetor, this would go in your functions.php file. Make sure you following the additional setup instructions on my first post of this thread:

    http://www.gravityhelp.com/forums/topic/dynamic-price-per-square-metre-calculation#post-16714

    Posted 13 years ago on Wednesday March 2, 2011 | Permalink

This topic has been resolved and has been closed to new replies.