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?
Answer
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>'; } }