当从 Woocommerce 检索订单信息时,它指出它应该是一个数组,请参阅..
http://docs.woothemes.com/wc-apidocs/class-WC_Order.html#_get_shipping_address
相反,它返回一个逗号分隔的字符串。
查看 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;
}
我可以看到它使用 implode 创建字符串,而不修改此函数,如何调用此函数并将 $address 作为数组检索? (如果这可能吗?)
原因是我可以分解返回的值,但地址中的逗号会破坏它。
最佳答案
回答自己问题的习惯...
因此,要接收如上所述的字符串,您可以这样做......
$order = new WC_Order($order_id);
echo $order->get_shipping_address(); //Road , Street, City, County, Postcode, Country
如果您想接收单独的线路,您可以这样做......
$order = new WC_Order($order_id);
echo $order->shipping_city; //London
echo $order->shipping_country; //GB
IMO 后者会更有用!
关于php - Woocommerce WC_Order get_shipping_address() 未作为数组返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22327474/