douhe5092 2014-03-11 13:43
浏览 49
已采纳

Woocommerce WC_Order get_shipping_address()不作为数组返回

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

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.

  • 写回答

1条回答 默认 最新

  • douyuan1752 2014-03-12 17:17
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用