user3467855 user3467855 -3 years ago 61
PHP Question

Reordering checkout fields in WooCommerce

I'm trying to re-order 2 custom checkout fields added with the help of

filter, just that when I apply
filter to re-order fields, it doesn't recognize them and they are

I think it's because the filter
goes after

How Can I solve this?

Here is my code:

add_action( 'woocommerce_checkout_init', 'wc_add_confirm_email_checkout', 10, 2 );
function wc_add_confirm_email_checkout( $checkout ) {
$checkout->checkout_fields['billing']['billing_email2'] = array(
'type' => 'text',
'label' => __( 'Confirm Email Address', 'woocommerce' ),
'required' => true,
'placeholder' => _x( 'Confirm Email Address', 'placeholder', 'woocommerce' )
add_action( 'woocommerce_checkout_init', 'wc_add_confirm_password_checkout', 10, 2 );
function wc_add_confirm_password_checkout( $checkout ) {
if ( get_option( 'woocommerce_registration_generate_password' ) == 'no' ) {
$checkout->checkout_fields['account']['account_password2'] = array(
'type' => 'password',
'label' => __( 'Confirm password', 'woocommerce' ),
'required' => true,
'placeholder' => _x( 'Confirm Password', 'placeholder', 'woocommerce' )

function reorder_woo_fields($fields) {
$fields2['billing']['billing_first_name'] = $fields['billing']['billing_first_name'];
$fields2['billing']['billing_last_name'] = $fields['billing']['billing_last_name'];
$fields2['billing']['billingooglg_email'] = $fields['billing']['billing_email'];
$fields2['billing']['billing_email2'] = $fields['billing']['billing_email2'];
$fields2['billing']['account_password'] = $fields['account']['account_password'];
$fields2['billing']['account_password2'] = $fields['account']['account_password2'];
$fields2['billing']['billing_address_1'] = $fields['billing']['billing_address_1'];
$fields2['billing']['billing_postcode'] = $fields['billing']['billing_postcode'];
//return $fields2;

Answer Source

I am not completely sure, but you there are some things that you can't do like merging billing fields with account fields. If you want to do that is going to be much more complicated than what you are trying to do here. In that case you will need to rewrite/create some checkout templates…

Another thing is that billing_email and billing_phone are sharing the same line together with 'class' => 'form-row-first' and 'class' => 'form-row-last'. When not this class is define 'class' => 'form-row-wide'… So you are going to need overriding these 'class' too.

After that you dont need to use 'woocommerce_checkout_init' hook…
You can still use 'woocommerce_checkout_fields'.
Also you can merge all of them in one function this way:

 * Creating, overriding and reordering custom fields.
add_filter( "woocommerce_checkout_fields", "custom_override_checkout_fields", 11, 1 );
function custom_override_checkout_fields( $fields ) {

    // Creating 'billing_email2' field
    $fields['billing']['billing_email2'] = array(
        'type'          => 'text',
        'label'         => __( 'Confirm Email Address', 'woocommerce' ),
        'placeholder'   => _x( 'Confirm Email Address', 'placeholder', 'woocommerce' ),
        'required'      => true,
        'class'         => array('form-row-last'),
        'clear'         => true

    // =======> I don't really know if you need this one  <========
    // it already exist (see in first reference link at bottom).

    // Creating 'account_password2' field 
    if ( get_option( 'woocommerce_registration_generate_password' ) == 'no' ) {
        $fields['account']['account_password2'] = array(
            'type'          => 'password',
            'label'         => __( 'Confirm password', 'woocommerce' ),
            'placeholder'   => _x( 'Confirm Password', 'placeholder', 'woocommerce' ),
            'required'      => true,
            'class'         => array('form-row-wide') //,
            // 'clear'         => true

    // Overriding existing billing_phone field 'class' property 
    $fields['billing']['billing_phone']['class'] = 'form-row-wide';

    // Reordering billing fields
    $order = array(

    foreach($order as $field)
        $ordered_fields[$field] = $fields["billing"][$field];

    $fields["billing"] = $ordered_fields;

    return $fields;

As I have said before, i think that you can't merge billing fields with account fields.
As 'account_password2' already exist if you refer to official documentation (see below in first reference link), you may not need to create it. You will have to test this and to fine tune it. But this is the way to do it.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download