Load Address from order
I'm completely unfamiliar with the Entity API and am struggling to get hold of some fairly straightforward data from the order object - the billing address.
I've tried the code snippet suggested by Ryan:
<?php
$order = commerce_order_load($order_id);
$wrapper = entity_metadata_wrapper('commerce_order', $order);
$address = $wrapper->commerce_customer_billing->commerce_customer_address->value();
?>
This gives me the following error:
EntityMetadataWrapperException: Unknown data property commerce_customer_address. in EntityStructureWrapper->getPropertyInfo() (line 328 of /[path to site]/profiles/commerce_kickstart/modules/entity/includes/entity.wrapper.inc).
I'm a little bit lost and I'm man enough to admit it. Who can give me a pointer?
Comments
I was having the same
I was having the same problem, with the exact same use case. It's hard to know the right structure, and even if you know the EntityAPI, its hard with the wrappers to know exactly what to call.
What you can do is call a getPropertyInfo() on the wrapper, and that will give you a list of the data you can call. You can also call that on any sub properties.
So call $wrapper->commerce_customer_billing->commerce_customer_address->getPropertyInfo() and that will give you the other properties you can call on that specific wrapper.
Just wrap it in a dpm() with the devel module for an easy way to browse it.
And here is the actual answer to your problem. This will get you each piece of the address you need (and tie them to array keys in this specific example):
'address1' => $order_wrapper->commerce_customer_billing->commerce_customer_address->thoroughfare->value(), 'address2' => $order_wrapper->commerce_customer_billing->commerce_customer_address->premise->value(),
'city' => $order_wrapper->commerce_customer_billing->commerce_customer_address->locality->value(),
'state' => $order_wrapper->commerce_customer_billing->commerce_customer_address->administrative_area->value(),
'country' => $order_wrapper->commerce_customer_billing->commerce_customer_address->country->value(),
'zip' => $order_wrapper->commerce_customer_billing->commerce_customer_address->postal_code->value(),
Get a nice array of billing info like this:
//load the order as entity to get billing info
$order_wrapper = entity_metadata_wrapper('commerce_order', $order_ID);
$billing_address = $order_wrapper->commerce_customer_billing->commerce_customer_address->value();
// Ensure we have a first and last name in the address.
if (empty($billing_address['first_name'])) {
$name_parts = explode(' ', $billing_address['name_line']);
$billing_address['first_name'] = array_shift($name_parts);
$billing_address['last_name'] = implode(' ', $name_parts);
}
// Ensure we have a state the address.
if (empty($billing_address['administrative_area'])) {
$billing_address['administrative_area'] = $billing_address['locality'];
}
$billing_info = array(
'firstName' => substr($billing_address['first_name'], 0, 50),
'lastName' => substr($billing_address['last_name'], 0, 50),
'company' => substr($billing_address['organisation_name'], 0, 50),
'address' => substr($billing_address['thoroughfare'], 0, 60),
'city' => substr($billing_address['locality'], 0, 40),
'state' => substr($billing_address['administrative_area'], 0, 40),
'zip' => substr($billing_address['postal_code'], 0, 20),
'country' => $billing_address['country'],
);