2

I'd like to add some data to a variable in a contributed module but I'm not sure how to do that with a custom module.

The Question:

The module I need to 'hack' is the Drupal Commerce PayPal WPP Module. I talked with PayPal and they said specifically I'd need to hack this file: 'commerce_paypal_wpp.module'. They said on line 365 if I added more data to the 'CUSTOM' variable (see below) it would pass that on to PayPal. Sweet! But how can I do that?

My Proposed Solution

I'm thinking I will create my variables to add to CUSTOM, like so (this is how I'd normally do it in PHP of course:

$fname = 'examplename';
$lname = 'examplelastname';
$arr = array($fname, $lname);
$allthedata = implode('+', $arr);
CUSTOM = $allthedata

It's that last line I don't know how to add properly! This, because 'CUSTOM' is already defined in the contributed module (see below).

The Function I Need to Edit:

The function in question in the contributed module is:

function commerce_paypal_wpp_submit_form_submit($payment_method, 
$pane_form, $pane_values, $order, $charge) { 

And the relevant code with 'CUSTOM' > the variable I want to change:

// Add additional transaction invormation to the request array.
  $nvp += array(
    // Order Information; we append the timestamp to the order number to 
allow
    // for multiple transactions against the same order.
    'INVNUM' => substr($order->order_number, 0, 127) . '-' . REQUEST_TIME,
    'CUSTOM' => substr(t('Order @number', array('@number' => $order-
>order_number)), 0, 256),
    'DESC' => substr(implode(', ', $description), 0, 127),

    // Customer Information
    'EMAIL' => substr($order->mail, 0, 127),
    'IPADDRESS' => substr($ip_address, 0, 15),
    'FIRSTNAME' => substr($billing_address['first_name'], 0, 25),
    'LASTNAME' => substr($billing_address['last_name'], 0, 25),
    'STREET' => substr($billing_address['thoroughfare'], 0, 100),
    'STREET2' => substr($billing_address['premise'], 0, 100),
    'CITY' => substr($billing_address['locality'], 0, 40),
    'STATE' => substr($billing_address['administrative_area'], 0, 40),
    'COUNTRYCODE' => $billing_address['country'],
    'ZIP' => substr($billing_address['postal_code'], 0, 20),
  );
}
  • Hey @Sage, is this question still on? Did you try the hook_commerce_paypal_api_request_alter I mentioned below? Cheers – Stefanos Petrakis Mar 23 '16 at 23:02
4

You will not need to 'hack' this module, you can use a legitimate hook to modify the $nvp array, hook_commerce_paypal_api_request_alter

At line 382 of the commerce_paypal_wpp.module, right after the part you mention, the whole array is passed to the commerce_paypal_api_request where the mentioned hook is called early on, to allow modifications from other modules.

Following your example code, your hook implementation would look like this:

function yourmodule_commerce_paypal_api_request_alter(&$nvp, $order, $payment_method) {
  ...
  $fname = 'examplename';
  $lname = 'examplelastname';
  $arr = array($fname, $lname);
  $allthedata = implode('+', $arr);
  $nvp['CUSTOM'] = $allthedata;
  ...
}

Hope this helps!

  • Yes, thank you for the solution, it works very elegantly! – Sage Mar 23 '16 at 23:06
  • Very welcome, glad to hear! – Stefanos Petrakis Mar 23 '16 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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