I have successfully added the phone number field in Ship to different address and the phone number is showing in back-end as well. However I am not receiving the phone number in email. Kindly Help
This code helps me to add field:
add_filter( 'woocommerce_checkout_fields', 'bbloomer_shipping_phone_checkout' ); function bbloomer_shipping_phone_checkout( $fields ) { $fields['shipping']['shipping_phone'] = array( 'label' => 'Phone', 'required' => true, 'class' => array( 'form-row-wide' ), ); return $fields; } add_action( 'woocommerce_admin_order_data_after_shipping_address', 'bbloomer_shipping_phone_checkout_display' ); function bbloomer_shipping_phone_checkout_display( $order ){ echo '<p><b>Shipping Phone:</b> ' . get_post_meta( $order->get_id(), '_shipping_phone', true ) . '</p>'; }
I have tried adding additional code in the above code (shown below) to show the phone number in email. Still doesn’t work!
add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 ); function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) { $fields['meta_key'] = array( 'label' => __( 'Shipping Phone' ), 'value' => get_post_meta( $order->id, '_shipping_phone', true ), ); return $fields; }
Advertisement
Answer
You’re missing the correct meta_key
, I have also slightly modified your existing code.
Note: by adjusting the priority
argument, you can set your field in the correct location.
So you get:
// Shipping field on my account edit-addresses and checkout function filter_woocommerce_shipping_fields( $fields ) { $fields['shipping_phone'] = array( 'label' => __( 'Shipping Phone', 'woocommerce' ), 'required' => true, 'class' => array( 'form-row-wide' ), 'priority' => 55 ); return $fields; } add_filter( 'woocommerce_shipping_fields' , 'filter_woocommerce_shipping_fields', 10, 1 ); // Display on the order edit page (backend) function action_woocommerce_admin_order_data_after_shipping_address( $order ) { if ( $value = $order->get_meta( '_shipping_phone' ) ) { echo '<p><strong>' . __( 'Shipping Phone', 'woocommerce' ) . ':</strong> ' . $value . '</p>'; } } add_action( 'woocommerce_admin_order_data_after_shipping_address', 'action_woocommerce_admin_order_data_after_shipping_address', 10, 1 ); // Display on email notifications function filter_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) { // Get meta $shipping_phone = $order->get_meta( '_shipping_phone' ); // NOT empty if ( ! empty( $shipping_phone ) ) { $fields['_shipping_phone'] = array( 'label' => __( 'Shipping Phone', 'woocommerce' ), 'value' => $shipping_phone, ); } return $fields; } add_filter( 'woocommerce_email_order_meta_fields', 'filter_woocommerce_email_order_meta_fields', 10, 3 );