andys
andys

Reputation: 708

Get shipping address from order, woo commerce

Hello I cant get from woocommerce order shipping address, I get error:

Fatal error: Call to a member function get_formatted_address() on a non-object in ..

I am using code:

$order_data = new WC_Order('12');

print_r($order_data->get_formatted_shipping_address());

Upvotes: 5

Views: 37940

Answers (4)

Radames E. Hernandez
Radames E. Hernandez

Reputation: 4425

You can get all the shipping address fields very simple, with this code that work for me:

Code:

$order = new WC_Order($order_id); // Order id
$shipping_address = $order->get_address('shipping'); 

print_r($shipping_address);

The function get_address() by default returns the billing address details but you can get the shipping address details passing the parameter 'shipping'

Here are the docs: Woocommerce docs

Regards!!

Upvotes: 7

user2462948
user2462948

Reputation: 99

Use this

    $order->get_shipping_address_1();    
    $order->get_shipping_address_2();

Upvotes: 0

Brian Dillingham
Brian Dillingham

Reputation: 9356

Here is a workaround

// 123 Happy Coding Lane, Apt #5 Delray Beach, FL 33445

function formatted_shipping_address($order)
{
    return
        $order->shipping_address_1 . ', ' . 
        $order->shipping_address_2 . ' ' .
        $order->shipping_city      . ', ' .
        $order->shipping_state     . ' ' .
        $order->shipping_postcode;
}

echo formatted_shipping_address($order);

You can access the shipping & billing properties directly.

Shipping

$order->shipping_first_name
$order->shipping_last_name
$order->shipping_company
$order->shipping_address_1
$order->shipping_address_2
$order->shipping_city
$order->shipping_state
$order->shipping_postcode
$order->shipping_country

Billing

$order->billing_first_name
$order->billing_last_name
$order->billing_company
$order->billing_address_1
$order->billing_address_2
$order->billing_city
$order->billing_state
$order->billing_postcode
$order->billing_country

Upvotes: 23

Domain
Domain

Reputation: 11808

Remove single quotes because WC_Order constructor accepts integer parameter order id.

 $order_data = new WC_Order(12);

 print_r($order_data->get_formatted_shipping_address());

Upvotes: 4

Related Questions