您可以通过 woocommerce_order_get_formatted_billing_address 过滤钩子修改WC_Order 类的get_formatted_billing_address 方法的输出(如您在Adding newly added fields in billing address and shipping address in order emails woocommerce 中所见)。
在您的情况下,添加自定义字段:
_billing_street
_billing_town
_billing_avenue
您可以使用以下功能:
随意更改计费字段的排序顺序(通过更改
$data 数组的值)。
// adds the custom fields to the formatted billing address
add_filter( 'woocommerce_order_get_formatted_billing_address', 'add_custom_field_billing_address', 10, 3 );
function add_custom_field_billing_address( $address, $raw_address, $order ) {
$countries = new WC_Countries();
// gets country and state codes
$billing_country = $order->get_billing_country();
$billing_state = $order->get_billing_state();
// gets the full names of the country and state
$full_country = ( isset( $countries->countries[ $billing_country ] ) ) ? $countries->countries[ $billing_country ] : $billing_country;
$full_state = ( $billing_country && $billing_state && isset( $countries->states[ $billing_country ][ $billing_state ] ) ) ? $countries->states[ $billing_country ][ $billing_state ] : $billing_state;
$data = array(
$order->get_billing_first_name() . ' ' . $order->get_billing_last_name(),
$order->get_billing_company(),
$order->get_billing_address_1(),
$order->get_billing_address_2(),
$order->get_billing_postcode(),
$order->get_billing_city(),
wc_strtoupper( $full_state ),
$order->get_meta( '_billing_street', true ),
$order->get_meta( '_billing_town', true ),
$order->get_meta( '_billing_avenue', true ),
);
// removes empty fields from the array
$data = array_filter( $data );
// create the billing address using the "<br/>" element as a separator
$address = implode( '<br/>', $data );
return $address;
}
代码已经过测试并且可以运行。将其添加到活动主题的 functions.php 中。
所以现在您可以使用$order->get_formatted_billing_address();
获取使用自定义字段格式化的帐单地址的方法
包括在内。