I need to use the following hook function to edit a similarly-named function in a contributed module (PayPal WPP), but I don't understand how to use it.

hook_commerce_paypal_api_request_alter(&$nvp, $order, $payment_method) { }

When this function is called I need to pass some variables to PayPal, based off user-supplied input in a form. By the time this function is called the user has already supplied their input in the form and clicked 'submit'. The form is now ready to send it's data to PayPal and this hook alters what is being sent.

So I need to figure out how to add additional data. Specifically I want to alter a variable $nvp['custom']. The main problem is I don't know how to get the values the user entered in the form into this function so I can pass them on.

I'm told I need to get the user-supplied values using $form_state['values'] but I have no idea how to get those inside this function, which isn't passed $form_state as a parameter. I was hoping to do something like this:

$name = $form_state['values']['field_name'];
$organization = $form_state['values']['field_organization'];
$arr = array($name, $organization);
$allthedata = implode('+', $arr);
$nvp['CUSTOM'] = $allthedata;

As it may be obvious I don't even know how to refer correctly to the passed $form_state values, which is probably a large part of my problem.


How do i get my user-supplied form values into this function so I can use them (and then pass them on to PayPal as part of the $nvp['custom'] variable?

1 Answer 1


You can user this code in which you can add your form data in array and pass it to the Paypal api.

function do_custom_commerce_paypal_api_request_alter(&$nvp, $order, $payment_method){
    if ($payment_method['base'] == 'commerce_paypal_wpp' ) { //check payment method

        //save form data
        $name = $form_state['values']['field_name'];
        $organization = $form_state['values']['field_organization'];

        $nvp += array(
        'name' => $name,
        'organization' => $organization,

Here is the similar solution for your problem in which user has passed order details. Click Here

  • Question: how is this function 'aware' of $form_state['values'] if it's not passed as a parameter? is it a persistent variable? it doesn't seem like it would be.
    – Sage
    Apr 21, 2016 at 5:56
  • @Sage : Every form has their own submit handler and you can save your form data in form_state['storage'] or your custom table table or you can get your data in an array and save it to variable table using drupal_get/drupal_put function for after use.
    – Manav
    Apr 21, 2016 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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