If you want to add a custom field to the order meta data, send the value of this field with the order confirmation mail, and additionally display it in the order detail and edit screen in the backend, you can use the follwing code. There are multiple steps to be done.
- Create a new field to show up in WooCommerce Checkout. Set it as required if you want to make sure, that there is a value entered. For this we are using
'woocommerce_after_checkout_billing_form'
. (just a sidenote: If you have other purposes, you can also i.e. use a hidden field and a given value)
- Save the value inside order meta data using
'woocommerce_checkout_update_order_meta'
- Add the value to the email being send after completing order using
'woocommerce_email_order_meta_keys'
- Show the value in the order detail screen in the backend using
'woocommerce_order_details_after_order_table'
and for the order edit screen 'woocommerce_admin_order_data_after_billing_address'
This will place it below the billing address. Notice: The value will not show up (but still be saved in database), if the order is made within the backend, only works for orders placed in the frontend (Would go beyond the scope now).
In my code example, I did this steps to add a VAT ID field which is important in europe for business to business transactions. The VAT ID is also added to the emails and backend screens.
You can adjust the names (vat_number, or the "mrank" prefixes) to your needs, but remember to keep it consistent.
/**
* VAT Number in WooCommerce Checkout
*/
function mrank_vat_field( $checkout ) {
echo '<div id="mrank_vat_field">';
woocommerce_form_field( 'vat_number', array(
'type' => 'text',
'class' => array( 'vat-number-field form-row-wide') ,
'label' => __( 'VAT-ID' ),
'placeholder' => __( 'Enter number' ),
'description' => __( 'Please enter your VAT-ID' ),
'required' => true,
), $checkout->get_value( 'vat_number' ));
echo '</div>';
}
add_action( 'woocommerce_after_checkout_billing_form', 'mrank_vat_field' );
/**
* Save VAT Number in the order meta
*/
function mrank_checkout_vat_number_update_order_meta( $order_id ) {
if ( ! empty( $_POST['vat_number'] ) ) {
update_post_meta( $order_id, '_vat_number', sanitize_text_field( $_POST['vat_number'] ) );
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'mrank_checkout_vat_number_update_order_meta' );
/**
* Display VAT Number in order details screen
*/
function mrank_vat_number_display_order_details($order){
echo '<p><strong>'.__('VAT-ID').':</strong> ' . get_post_meta( $order->get_id(), '_vat_number', true ) . '</p>';
}
add_action( 'woocommerce_order_details_after_order_table', 'mrank_vat_number_display_order_details', 10, 1 );
/**
* Display VAT Number in order edit screen
*/
function mrank_vat_number_display_admin_order_meta( $order ) {
echo '<p><strong>' . __( 'VAT-ID', 'woocommerce' ) . ':</strong> ' . get_post_meta( $order->get_id(), '_vat_number', true ) . '</p>';
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'mrank_vat_number_display_admin_order_meta', 10, 1 );
/**
* VAT Number in emails
*/
function mrank_vat_number_display_email( $keys ) {
$keys['VAT-ID'] = '_vat_number';
return $keys;
}
add_filter( 'woocommerce_email_order_meta_keys', 'mrank_vat_number_display_email' );
code#
the place where you use wp_mail ? Or maybe you want to change the default mail being send by woocommerce? – rank