WooCommerce 将订单送货地址作为单独的字段
WooCommerce get order shipping address as separate fields
我正在构建一个向第三方发送订单的 WooCommerce 插件 api。
但是,我发现自 WooCommerce v2.3 以来,没有办法获取订单送货地址的个人 fields/properties。唯一可用的函数是
get_formatted_shipping_address()
returns 一个字符串,returns 一个地址数组 "get_shipping_address()" 的函数似乎在 2.3 版中已被弃用。
有谁知道如何获取订单的送货地址数组?我真的不想求助于使用旧版本的 WooCommerce。也许有一个钩子、动作或 class 覆盖我可以用来实现这个?
看一下class'WC_Api_Orders'
的'get_order'函数
'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,
),
您可以直接访问订单的属性。
我会查看 WC_Abstract_Order 这两个 public 函数。
/**
* 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();
}
希望这对您有所帮助,
蒂姆
我通过这种简单的方式获得了送货地址:
function get_shipping_zone(){
global $woocommerce;
$post_code = $woocommerce->customer->get_shipping_postcode();
$zone_postcode = $woocommerce->customer->get_shipping_postcode();
$zone_city =get_shipping_city();
$zone_state = get_shipping_state();
}
您还可以print_r为“$woocommerce->customer”获取您可能需要的所有元数据,了解它非常有用。
我正在构建一个向第三方发送订单的 WooCommerce 插件 api。
但是,我发现自 WooCommerce v2.3 以来,没有办法获取订单送货地址的个人 fields/properties。唯一可用的函数是
get_formatted_shipping_address()
returns 一个字符串,returns 一个地址数组 "get_shipping_address()" 的函数似乎在 2.3 版中已被弃用。
有谁知道如何获取订单的送货地址数组?我真的不想求助于使用旧版本的 WooCommerce。也许有一个钩子、动作或 class 覆盖我可以用来实现这个?
看一下class'WC_Api_Orders'
的'get_order'函数 '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,
),
您可以直接访问订单的属性。
我会查看 WC_Abstract_Order 这两个 public 函数。
/**
* 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();
}
希望这对您有所帮助,
蒂姆
我通过这种简单的方式获得了送货地址:
function get_shipping_zone(){
global $woocommerce;
$post_code = $woocommerce->customer->get_shipping_postcode();
$zone_postcode = $woocommerce->customer->get_shipping_postcode();
$zone_city =get_shipping_city();
$zone_state = get_shipping_state();
}
您还可以print_r为“$woocommerce->customer”获取您可能需要的所有元数据,了解它非常有用。