Change the description on a Gravity Forms PayPal donation

Gravity Forms plus its PayPal add-on make for a very easy donations form (and so does my Gravity Forms eWAY add-on). But the PayPal item description is pretty formulaic, and probably doesn’t represent the donation very well especially when there are multiple options. A simple filter hook with a few lines of code can easily fix that though.

The PayPal add-on has a filter hook, gform_paypal_query, that allows you to modify the payment part of the query string sent to PayPal. It has another filter hook, gform_paypal_request, for modifying the whole query string, but we only need to be concerned with the payment bits.

Gravity Forms PayPal donations are very simple, which is great because it means they’re very easy to modify. The query string given to our filter hook will look something like this:

&amount=100&item_name=Donation+amount&cmd=_donations

All we need to do is split it apart using parse_str(), change the item_name, and put it back together again using http_build_query(). Here’s a code snippet for that:

add_filter('gform_paypal_query', 'gfDonationPayPalQuery', 10, 3);

/**
* amend PayPal donation query string
* @param string $query_string the PayPal query string for the donation
* @param array $form
* @param array $entry
* @return string
*/
function gfDonationPayPalQuery($query_string, $form, $entry) {
    // break the query string into an array
    // after removing the initial '&' character
    parse_str(ltrim($query_string, '&'), $query);

    // set the new item name, e.g. from form values
    $query['item_name'] = "anything you'd like to build";

    // put it all back together again
    $query_string = '&' . http_build_query($query);

    return $query_string;
}

So how do you build the new item_name? The simplest way is to grab the values of fields in your form and add them into a formatted string. Here’s one I prepared earlier today. NB: you need to know the field IDs for the fields in your form, which you can get from the form editor.

$project = rgpost('input_1');
$country = rgpost('input_4');
$amount = GFCommon::to_number(rgpost('input_8'));
$query['item_name'] = sprintf('Donate $%s for %s in %s',
    number_format($amount), $project, $country);

And job is not only done, but we now know which job and for whom!

Facebooktwittergoogle_plusredditlinkedinmailFacebooktwittergoogle_plusredditlinkedinmail
  • Jonathan Garces

    Is there any way to grab the field label and use that as the Item Name?