Send Billing Information from Address Field to PayPal site using WPS Referring to the solution here posted by Fnik, I'm trying to implement it on my own custom module.  But I thing our two sites are configured differently, and since I'm not too familiar with php, I don't get how to adapt his code for my project.

I use Address Field module for customers billing and shipping address.  This doc page explains how it stores its subfields.  So I need to access these subfields with the hook Fnik pointed.

My code doesn't do it correctly on checkout. Although syntax seems ok, I get errors (specified bellow) when the function is called.  Here is my code:

function my_module_commerce_paypal_wps_order_form_data_alter(&amp;$data, $order) {
$profile = user_load($order-&gt;uid);
$data['first_name'] = $profile-&gt;field_commerce_customer_shipping-&gt;commerce_customer_address-&gt;first_name['und'][0]['value'];
$data['last_name'] = $profile-&gt;field_commerce_customer_shipping-&gt;commerce_customer_address-&gt;last_name['und'][0]['value'];
$data['address1'] = $profile-&gt;field_commerce_customer_shipping-&gt;commerce_customer_address-&gt;thoroughfare['und'][0]['value'];
}


Some context:


commerce_paypal_wps_order_form_data_alter is a hook from commerce_paypal_wps.module
field_commerce_customer_shipping is a Customer profile reference in Order type order entity.


commerce_customer_address is a Postal address field type in Shipping information Profile type entity.



Erros, 3 different errors for each $data line:


Undefined property: stdClass::$field_commerce_customer_shipping in my_module_commerce_paypal_wps_order_form_data_alter() (line 27 of ...)
Trying to get property of non-object in my_module_commerce_paypal_wps_order_form_data_alter() (line 27 of ...)
Trying to get property of non-object in my_module_commerce_paypal_wps_order_form_data_alter() (line 27 of ...)


Is is the $profile variable declaration that is wrong right in the beginning, or is it the way to refer to fields and subfields?

Thanks in advanced.
The user profile may not have the data saved to it yet, if it is a new user or if you allow anonymous checkout somehow, so it is better to retrieve this data directly from the order. To do that, we have to do some fancy footwork using the entity API - this also avoids any snafus related to database changes or translation arrays. I created a simple function that uses this API to extract an array of address fields:

/**
 * Helper to retrieve the profile from an order
 *
 * @param order
 *   The order object
 * @param type
 *   Either shipping or billing
 * @return
 *   The requested data
 */
function _my_module_address_from_order($order, $type="billing") {
  $wrapper = entity_metadata_wrapper('commerce_order', $order);
  $type = "commerce_customer_".$type;
  return $wrapper-&gt;$type-&gt;commerce_customer_address-&gt;value();
}


Then I call it thusly in the actual hook:

/**
 * Send default billing address to paypal
 */
function my_module_commerce_paypal_wps_order_form_data_alter(&amp;$data, $order) {
  $address = _my_module_address_from_order($order);
  $data['address1'] = $address['thoroughfare'];
  $data['address2'] = $address['premise'];
  $data['first_name'] = $address['first_name'];
  $data['last_name'] = $address['last_name'];
  $data['city'] = $address['locality'];
  $data['state'] = $address['administrative_area'];
  $data['zip'] = $address['postal_code'];
  $data['email'] = $order-&gt;mail;
}


Note that paypal will not display a billing address unless it gets all of the values address1, city, state and zip as well as first_name and last_name!

Much of this solution was arrived at from consulting http://www.drupalcommerce.org/discussions/756/load-address-order