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.

Lookup order info? or running a query and posting result back to the form?

  1. cakelian
    Member

    I'm posting as my last option because I've searched and tried different hooks and code and I can't seem to get this to work. I'm using GF with WooCommerce to process a form.

    All I want to do is to have Customer A place an order (without payment). Customer B will then go on, enter the order number to find out the cost, then go on to payment.

    The last thing I tried is using the below code in the functions.php file but no luck:

    [php]
    add_filter('gform_field_value_cst', 'populate_order_fee');
    function populate_order_fee($value){
        global $post;
    	global $wpdb;
    
    	$order_ID = $wpdb->get_var("SELECT post_id FROM database_table WHERE meta_value = '$_POST[ord];'");
    	$order_total = $wpdb->get_var("SELECT meta_value FROM database_table WHERE post_id = $order_ID AND meta_key = '_order_total'");
    
        return $order_total;
    }

    "cst" is the parameter name for the field that needs $order_total.

    Am I totally going about this the wrong way? Can GF even do this type of thing?
    Basically I want to fill out the first field of a 1 field form -> run a mysql query to find the $order_total -> populate the second second field -> then go to payment.

    Thanks in advance for any help.

    Posted 11 years ago on Sunday February 17, 2013 | Permalink
  2. Can you try some debug statements to see where this is failing? How about right after line 6, checking to see what $order_ID contains, and then after line 7, what does $order_total contain?

    Another approach would be to set $order_total to some value in your code, rather than getting it from the database, and see if your parameter is pre-filled in the form when it's rendered. Let us know what you find out.

    Posted 11 years ago on Monday February 18, 2013 | Permalink
  3. cakelian
    Member

    Hi Chris,

    I'm not sure I know how to run "debug statements" but when I run the code on its own (ie a separate script) and hard code the "$_POST[ord];" the query returns the $order_total.

    So for example if I run:
    $order_ID = $wpdb->get_var("SELECT post_id FROM database_table WHERE meta_value = 'order_1234567");

    It works.

    Also when I run:

    $order_number = $wpdb->get_var("SELECT meta_value FROM database_table WHERE post_id = '151' AND meta_key = '_order_key'");

    It will return the order number. It's labeled "_order_key" in the database.
    So....I can get the query to work on its own, just not when included in the functions.php file.

    Is it that the POST parameters are not being passed on?
    Thanks.

    Posted 11 years ago on Monday February 18, 2013 | Permalink
  4. Going back to my original reply: the debug statements I was referring to are to simply echo the $order_ID and $order_total right after you believe you are setting the values, to see what they contain. Can you do that? The text will just be displayed on screen, normally at the top of the page, but this is just temporary to see if they are being retrieved correctly.

    My second suggestion was to manually set $order_total to some value on line 7, rather than get it from the database, and then see if the value is populated corrected in your form.

    There are quite a few pieces to make work together and we need to figure out where it's breaking down, hence the need to some debugging steps along the way.

    Posted 11 years ago on Monday February 18, 2013 | Permalink