简体   繁体   中英

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

In woocommerce order emails i want to custom fields added along with billing address.This is what i am tried.But it is giving error.

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

anybody pls help.

You can use the woocommerce_order_get_formatted_billing_address filter instead of woocommerce_order_formatted_billing_address to make sure you change the final result of the billing address.

For the billing_area , billing_emirates and billing_nearest_landmark custom fields you will need to get their respective values as order 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.

The following code should work.

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;

}

The code has been tested and works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM