在订单电子邮件 woocommerce 中添加新添加的账单地址和送货地址字段

Adding newly added fields in billing address and shipping address in order emails woocommerce

在 woocommerce 订单电子邮件中,我想自定义字段与账单一起添加 address.This 就是我 tried.But 它给出错误。

add_filter('woocommerce_order_formatted_billing_address', array($this, 'woo_custom_order_formatted_billing_address'), 10, 2);
function woo_custom_order_formatted_billing_address( $address , $WC_Order ) {
    $address = array(
        'first_name'       => $WC_Order->billing_first_name . ' ' . $WC_Order->billing_last_name,
        'company'          => $WC_Order->billing_company,
        'address_1'        => $WC_Order->billing_address_1,
        'address_2'        => $WC_Order->billing_address_2,
        'city'             => $WC_Order->billing_city,
        'state'            => $WC_Order->billing_state,
        'area'             => $WC_Order->billing_area,
        'emirates'         => $WC_Order->billing_emirates,
        'nearest_landmark' => $WC_Order->billing_nearest_landmark,
        'country'          => $WC_Order->billing_country,
        'email'            => $WC_Order->billing_email,
        'phone'            => $WC_Order->billing_phone,
    );
    return $address;
}

任何人都请帮助。

您可以使用woocommerce_order_get_formatted_billing_address filter instead of woocommerce_order_formatted_billing_address来确保您更改账单地址的最终结果。

对于 billing_areabilling_emiratesbilling_nearest_landmark 自定义字段,您需要获取它们各自的值作为订单 postmeta。

The sorting of the fields will also be applied in the display of the billing address in the backend and in the frontend.

The billing phone and billing email fields are added after the billing address in the /woocommerce/emails/email-addresses.php email template. So there is no need to add them otherwise they will show up double.

下面的代码应该可以工作。

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_city(),
        $full_state,
        $order->get_meta( '_billing_area', true ),             // or $order->get_meta( 'billing_area', true )
        $order->get_meta( '_billing_emirates', true ),         // or $order->get_meta( 'billing_emirates', true )
        $order->get_meta( '_billing_nearest_landmark', true ), // or $order->get_meta( 'billing_nearest_landmark', true )
        $full_country,
    );

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

}

代码已经过测试并且有效。