Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce get order shipping address as separate fields

I'm building a WooCommerce plugin which sends orders to a 3rd party api.

However, I have found there isn't a way to get the individual fields/properties of the order shipping address since WooCommerce v2.3. The only function available is

get_formatted_shipping_address()

Which returns a string and it seems the function which returns an address array "get_shipping_address()" has been Deprecated in version 2.3.

Does anyone know a way to get the shipping address as an array for an order? I don't really want to resort to using an old version of WooCommerce. Perhaps there is a hook, action or class override I can use to achieve this?

like image 570
Tom Avatar asked Oct 23 '25 14:10

Tom


2 Answers

Take a look at the 'get_order' function of class 'WC_Api_Orders'

        'shipping_address' => array(
            'first_name' => $order->shipping_first_name,
            'last_name'  => $order->shipping_last_name,
            'company'    => $order->shipping_company,
            'address_1'  => $order->shipping_address_1,
            'address_2'  => $order->shipping_address_2,
            'city'       => $order->shipping_city,
            'state'      => $order->shipping_state,
            'postcode'   => $order->shipping_postcode,
            'country'    => $order->shipping_country,
        ),

You can just directly access the properties of the order.

like image 180
Josh Koberstein Avatar answered Oct 26 '25 05:10

Josh Koberstein


I got the shipping address in this simple way :

function get_shipping_zone(){     

global $woocommerce;
$post_code = $woocommerce->customer->get_shipping_postcode();
$zone_postcode = $woocommerce->customer->get_shipping_postcode();
$zone_city =get_shipping_city(); 
$zone_state = get_shipping_state(); 

}

You can also print_r for the "$woocommerce->customer" you will get the all meta data you may need, it is very useful to know it.

like image 21
Mustafa ELnagar Avatar answered Oct 26 '25 04:10

Mustafa ELnagar