12

I need to get the shipping address (shipping country to be precise) programmatically in drupal commerce. I have the $order object. How can i get the shipping address?

EDIT - Ok i did this

 $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
 $shipping =  $order_wrapper->commerce_customer_shipping->value();

Now i need to wrap it up again, but i don't know the type

$shipping_wrapper = entity_metadata_wrapper(?, $order);

Whatshould i put instead of the question mark?

3 Answers 3

7

Ok i did this in this way

function commerce_shipping_biagetti_service_rate_order($shipping_service, $order) {
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  $shipping = $order_wrapper->commerce_customer_shipping->commerce_customer_address->value();
  //$shipping is an array containing all shipping data
1
  • 1
    Did you manage to get the $order object in your custom module? if so can you tell me how? Jan 24, 2014 at 11:57
1

you can use commerce_customer_profile_load($profile_id), the profile id can be fetched from $order->commerce_customer_shipping variable since you have the order object.

1

There are 2 Ways to Find Customer/user shipping Address-

function get_user_shipping_address(){

global $user; 
$default_pid =commerce_addressbook_get_default_profile_id($user->uid,'shipping');

After getting profile id you can load profile and get customer name & Address

$profile_load = commerce_customer_profile_load($default_pid);
$first_line = $profile_load->commerce_customer_address['und'][0]['name_line'];
$landmark = $profile_load->commerce_customer_address['und'][0]['sub_premise'];
$postal_code = $profile_load->commerce_customer_address['und'][0]['postal_code'];
$state = $profile_load->commerce_customer_address['und'][0]['locality'];
$add[] = $first_line . ' ' . $landmark . ' ' . $postal_code . ' ' . $state;
return $add;
}

second way if you have $order

function get_default_address_of_customer_by_order_id($order) {
  $order1 = commerce_order_load($order);
  $shipping_id = $order1->commerce_customer_shipping['und'][0]['profile_id'];
  $address = commerce_customer_profile_load($shipping_id);
  $first_line = $address->commerce_customer_address['und'][0]['name_line'];
  $landmark = $address->commerce_customer_address['und'][0]['sub_premise'];
  $postal_code = $address->commerce_customer_address['und'][0]['postal_code'];
  $state = $address->commerce_customer_address['und'][0]['locality'];
  $add[] = $first_line . ' ' . $landmark . ' ' . $postal_code . ' ' . $state;
  return $add;
 }

Your Answer

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

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