我正在构建一个WooCommerce插件,可以将订单发送给第三方API。
但是,我发现自WooCommerce v2.3起,无法获取订单送货地址的各个字段/属性。唯一可用的功能是
get_formatted_shipping_address()
返回一个字符串,似乎返回地址数组“get_shipping_address()”的函数已在版本2.3中弃用。
有没有人知道如何将送货地址作为订单的数组?我真的不想使用旧版本的WooCommerce。也许我可以使用钩子,动作或类覆盖来实现这个目标?
答案 0 :(得分:4)
看看' get_order'类的功能' 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,
),
您可以直接访问订单的属性。
答案 1 :(得分:4)
我以这种简单的方式获得了送货地址:
function get_shipping_zone(){
global $woocommerce;
$customer = new WC_Customer();
$post_code = $woocommerce->customer->get_shipping_postcode();
$zone_postcode = $woocommerce->customer->get_shipping_postcode();
$zone_city =get_shipping_city();
$zone_state = get_shipping_state();
}
您还可以打印“$ woocommerce->客户”的print_r,您将获得所需的所有元数据,了解它非常有用。
答案 2 :(得分:1)
我会查看WC_Abstract_Order以了解这两个公共功能。
/**
* 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;
}
而且......
/**
* 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();
}
希望这有帮助,
添