1

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(&$data, $order) {
$profile = user_load($order->uid);
$data['first_name'] = $profile->field_commerce_customer_shipping->commerce_customer_address->first_name['und'][0]['value'];
$data['last_name'] = $profile->field_commerce_customer_shipping->commerce_customer_address->last_name['und'][0]['value'];
$data['address1'] = $profile->field_commerce_customer_shipping->commerce_customer_address->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.

2
  • What is the name of your module? Commented Jan 21, 2014 at 19:48
  • Actualy, it is not named my_module. It is send_paypal_address But I used my_module here intending to be more generic and clearer. Not sure if it was. Thanks
    – Myriade
    Commented Mar 31, 2014 at 21:18

1 Answer 1

1

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->$type->commerce_customer_address->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(&$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->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

1

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.