Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to augment a value to the wp_user_meta table, utilizing Gravity Forms. And I'm not having any luck. I had a version of this working on gform_pre_submission (though without the class-sniffing) but I want to do this after submission since there will be PayPal involved and don't want to post until the payment is verified.

I was trying to use cssClass as a filter so the other developers are free to create as many gravity forms as they want, provided they use the classes product_name and product_price

Unfortunately, my database is just filling up with fails right now.

// hook onto gravity forms and update payment history field
add_action('gform_after_submission', 'payment_history_handler', 10, 2);
function payment_history_handler($entry, $form){

$current_user = wp_get_current_user();
$payment_history = get_user_meta($current_user->ID, 'payment_history', true);

foreach($form['fields'] as &$field){
    if(strpos($field['cssClass'], 'payment_name') === true){
        $payment_name = $entry[$field['id']];;
    } else {
        $payment_name = 'fail';
    }
    if(strpos($field['cssClass'], 'payment_price') === true){
        $payment_price = $entry[$field['id']];
    } else {
        $payment_price = 1;
    }
}

if(!empty($payment_name) && !empty($payment_price)){
    $payment_history[] = array('date' => date('U'), 'name' => $payment_name, 'price' => $payment_price);
    update_user_meta($current_user->ID, 'payment_history', $payment_history);
}

}

The classes are in place, as seen by this sample of my output:

<div class='gform_body'>
  <ul id='gform_fields_4' class='gform_fields top_label description_below'>
    <li id='field_4_1' class='gfield    payment_name           gfield_contains_required' >
      <label class='gfield_label' for='input_4_1'>What Thing are you choosing?<span class='gfield_required'>*</span></label>
      <div class='ginput_container'>
        <select name='input_1' id='input_4_1'  class='medium gfield_select' tabindex='1' >
          <option value='The first one' >The first one</option>
          <option value='The second one' >The second one</option>
          <option value='The third one' >The third one</option>
        </select>
      </div>
    </li>
    <li id='field_4_2' class='gfield    payment_price           gfield_contains_required' >
      <label class='gfield_label' for='input_4_2'>Price<span class='gfield_required'>*</span></label>
      <div class='ginput_container'>
        <input name='input_2' id='input_4_2' type='text'  value='' class='medium' tabindex='2'   />
      </div>
    </li>
  </ul>
</div>

Any help is greatly appreciated.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

It looks like your code is testing to see where every field on the form has class 'payment_name'. If you instead only want to find one field with class 'payment_name', then you need to break out of the for loop once you've found that field.

Also, strpos only returns false or an integer index into the haystack where it found the needle, not true; test instead for strpos($field['cssClass'], 'payment_name') !== false

share|improve this answer
    
Nice. This is working now. I'm actually looking for payment_name in one field and payment_price in another. I suppose once both of these are defined I can abort. Thanks. –  GhostToast Jan 10 '13 at 22:59
    
In case anyone finds this, a good way to break was to move the if(!empty()) part of the code up into the loop and add a break statement beneath it, so as soon as both variables are defined, it does the work and stops looking. Thanks again @webaware –  GhostToast Jan 10 '13 at 23:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.