Woocommerce WC_Order get_shipping_address() not returning as array

When retrieving order information from Woocommerce it states it should be an array see..

http://docs.woothemes.com/wc-apidocs/class-WC_Order.html#_get_shipping_address

Read More

Instead it returns a comma separated string.

Looking at the function in the core code of Woocommerce…

 public function get_shipping_address() {
  if ( ! $this->shipping_address ) {
      if ( $this->shipping_address_1 ) {
          // Formatted Addresses
                $address = array(
                '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
                );
         $joined_address = array();
            foreach ( $address as $part ) {
                if ( ! empty( $part ) ) {
                    $joined_address[] = $part;
                }
            }
            $this->shipping_address = implode( ', ', $joined_address );
        }
 }
  return $this->shipping_address;
 }

I can see it using implode to create the string, without modifying this function how can I call this function and retrieve $address as an array? (if this is possible?)

The reason is I CAN explode the returned value but commas in the address will break it.

Related posts

Leave a Reply

1 comment

  1. Habit of answering my own questions…

    So to receive a string as explain above you would do it like this…

    $order = new WC_Order($order_id);
    echo $order->get_shipping_address();  //Road , Street, City, County, Postcode, Country
    

    If you want to receive individual lines you can do so like this…

    $order = new WC_Order($order_id);
    echo $order->shipping_city;      //London
    echo $order->shipping_country;   //GB
    

    IMO the later is going to be a lot more useful!