To add a simple text field after the address information you will need a simple code you can adjust placed inside your functions.php file.
1. This will add a extra field (call direct debit)
2. This will also be added to the woocommerce emails that get emailed out to the customer and admin
3. If you need this to be compulsory then change line 14 to the following ‘required’ => true
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
// test replace 'directdeb' add_action( 'woocommerce_before_order_notes', 'codehaven_add_custom_checkout_field' ); function codehaven_add_custom_checkout_field( $checkout ) { $current_user = wp_get_current_user(); $saved_directdeb = $current_user->directdeb; woocommerce_form_field( 'directdeb', array( 'type' => 'text', 'class' => array( 'form-row-wide' ), 'label' => 'Direct Debit Information', 'placeholder' => 'Completion required if paying monthly or purchasing a service plan', 'required' => false, 'default' => $saved_directdeb, ), $checkout->get_value( 'directdeb' ) ); } add_action( 'woocommerce_checkout_update_order_meta', 'codehaven_save_new_checkout_field' ); function codehaven_save_new_checkout_field( $order_id ) { if ( $_POST['directdeb'] ) update_post_meta( $order_id, '_directdeb', esc_attr( $_POST['directdeb'] ) ); } add_action( 'woocommerce_admin_order_data_after_billing_address', 'codehaven_show_new_checkout_field_order', 10, 1 ); function codehaven_show_new_checkout_field_order( $order ) { $order_id = $order->get_id(); if ( get_post_meta( $order_id, '_directdeb', true ) ) echo '<p><strong>Direct Debit details:</strong> ' . get_post_meta( $order_id, '_directdeb', true ) . '</p>'; } add_action( 'woocommerce_email_after_order_table', 'codehaven_show_new_checkout_field_emails', 20, 4 ); function codehaven_show_new_checkout_field_emails( $order, $sent_to_admin, $plain_text, $email ) { if ( get_post_meta( $order->get_id(), '_directdeb', true ) ) echo '<p><strong>Direct Debit details:</strong> ' . get_post_meta( $order->get_id(), '_directdeb', true ) . '</p>'; } |
If you are looking to validate this – take a look here