Tom
Tom

Reputation: 406

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?

Upvotes: 4

Views: 12306

Answers (3)

Mustafa ELnagar
Mustafa ELnagar

Reputation: 492

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.

Upvotes: 4

I'd look at WC_Abstract_Order for both of these public functions.

    /**
     * Get a formatted shipping address for the order.
     *
     * @return string
     */
    public function get_formatted_shipping_address() {
        if ( ! $this->formatted_shipping_address ) {

            if ( $this->shipping_address_1 || $this->shipping_address_2 ) {

                // Formatted Addresses
                $address = apply_filters( 'woocommerce_order_formatted_shipping_address', array(
                    'first_name'    => $this->shipping_first_name,
                    'last_name'     => $this->shipping_last_name,
                    'company'       => $this->shipping_company,
                    'address_1'     => $this->shipping_address_1,
                    'address_2'     => $this->shipping_address_2,
                    'city'          => $this->shipping_city,
                    'state'         => $this->shipping_state,
                    'postcode'      => $this->shipping_postcode,
                    'country'       => $this->shipping_country
                ), $this );

                $this->formatted_shipping_address = WC()->countries->get_formatted_address( $address );
            }
        }

        return $this->formatted_shipping_address;
    }

And ...

    /**
     * Calculate shipping total.
     *
     * @since 2.2
     * @return float
     */
    public function calculate_shipping() {

        $shipping_total = 0;

        foreach ( $this->get_shipping_methods() as $shipping ) {
            $shipping_total += $shipping['cost'];
        }

        $this->set_total( $shipping_total, 'shipping' );

        return $this->get_total_shipping();
    }

Hope this helps,

Tim

Upvotes: 2

Josh Koberstein
Josh Koberstein

Reputation: 176

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.

Upvotes: 6

Related Questions