简体   繁体   中英

Show billing email from customer orders in WooCommerce My account Edit address

I Want to show meta "email" of a customers each order, but this is throwing an error.

add_action( 'woocommerce_before_edit_account_address_form', 'save_billing_email_to_user', 12, 1 );
function save_billing_email_to_user( $user_id ) {
    $query = new WC_Order_Query( 
        array( 
            'customer_id' => $user_id 
        )
    );
    $orders = $query->get_orders();
    foreach($orders as $ro => $inn){
             echo $ro['email'];
    }
}

What Im doing wrong here?

Use instead the following revisited code that will query one order to get the billing email:

add_action( 'woocommerce_before_edit_account_address_form', 'save_billing_email_to_user', 12 );
function save_billing_email_to_user( $user_id ) {
    $orders = wc_get_orders(  array( 'customer_id' => $user_id , 'limit' => '1' ) );
    if ( ! empty( $orders ) ) {
        $order = reset( $orders );
        $order_id      = $order->get_id();
        $billing_email = $order->get_billing_email();
        echo $billing_email;
    }
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.


Or you can show the billing email address from each order of a customer like:

add_action( 'woocommerce_before_edit_account_address_form', 'save_billing_email_to_user', 12 );
function save_billing_email_to_user( $user_id ) {
    $orders = wc_get_orders(  array( 'customer_id' => $user_id ) );
    foreach( $orders as $order ){
        $order_id = $order->get_id();
        echo $order->get_billing_email() . '<br>';
    }
}

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