Move Checkout Order notes field above "create an account" in WooCommerce 3

Viewed 3277

I am using WooCommerce version 3.2.1 and I've tried to add the code below in my theme functions.php to move Order notes Checkout field and but it's not working:

add_filter( 'woocommerce_checkout_fields', 'bbloomer_move_checkout_fields_woo_3');
  function bbloomer_move_checkout_fields_woo_3( $fields ) {
  $fields['order']['order_comments']['priority'] = 8;
  return $fields;
}

I would like to move Order notes textarea field above "create_account" checkbox and under the "billing_postcode" field on the Checkout page.

How can I make it work?

1 Answers

In the code below:

  • I remove the "Order notes"
  • I add a similar custom billing field (named "billing_customer_note")
  • I reorder the fields (*)
  • Once the Order is submitted in checkout, I add the custom billing field "customer note" data as a classic order note…

Here is that code:

// Checkout fields customizations
add_filter( 'woocommerce_checkout_fields' , 'customizing_checkout_fields', 10, 1 );
function customizing_checkout_fields( $fields ) {
    // Remove the Order Notes
    unset($fields['order']['order_comments']);

    // Define custom Order Notes field data array
    $customer_note  = array(
        'type' => 'textarea',
        'class' => array('form-row-wide', 'notes'),
        'label' => __('Order Notes', 'woocommerce'),
        'placeholder' => _x('Notes about your order, e.g. special notes for delivery.', 'placeholder', 'woocommerce')
    );

    // Set custom Order Notes field
    $fields['billing']['billing_customer_note'] = $customer_note;

    // Define billing fields new order
    $ordered_keys = array(
        'billing_first_name',
        'billing_last_name',
        'billing_company',
        'billing_country',
        'billing_address_1',
        'billing_address_2',
        'billing_city',
        'billing_state',
        'billing_postcode',
        'billing_phone',
        'billing_email',
        'billing_customer_note', // <= HERE
    );

    // Set billing fields new order
    $count = 0;
    foreach( $ordered_keys as $key ) {
        $count += 10;
        $fields['billing'][$key]['priority'] = $count;
    }

    return $fields;
}

// Set the custom field 'billing_customer_note' in the order object as a default order note (before it's saved)
add_action( 'woocommerce_checkout_create_order', 'customizing_checkout_create_order', 10, 2 );
function customizing_checkout_create_order( $order, $data ) {
    $order->set_customer_note( isset( $data['billing_customer_note'] ) ? $data['billing_customer_note'] : '' );
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and works.

Related