Woocommerce WC_Order get_shipping_address() 不作为数组返回 [英] Woocommerce WC_Order get_shipping_address() not returning as array

查看:25
本文介绍了Woocommerce WC_Order get_shipping_address() 不作为数组返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从 Woocommerce 检索订单信息时,它指出它应该是一个数组,请参阅..

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.

看Woocommerce核心代码中的函数...

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;
 }

我可以看到它使用内爆创建字符串,而无需修改此函数,如何调用此函数并将 $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.

推荐答案

回答自己问题的习惯...

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 后者会更有用!

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

这篇关于Woocommerce WC_Order get_shipping_address() 不作为数组返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆