0

I have reordered and changed the WooCommerce checkout fields with this:

//Reorder checkout fields   
add_filter( 'woocommerce_checkout_fields', 'reorder_woo_fields' ); 
    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']['billing_address_1']  = $fields['billing']['billing_address_1'];
    $fields2['billing']['billing_city']       = $fields['billing']['billing_city'];
    $fields2['billing']['billing_postcode']   = $fields['billing']['billing_postcode'];
    $fields2['billing']['billing_email']      = $fields['billing']['billing_email'];
    $fields2['billing']['billing_phone']      = $fields['billing']['billing_phone'];
    $fields2['billing']['billing_country']    = $fields['billing']['billing_country'];

    $fields2['shipping']['shipping_first_name'] = $fields['shipping']['shipping_first_name'];
    $fields2['shipping']['shipping_last_name'] = $fields['shipping']['shipping_last_name'];
    $fields2['shipping']['shipping_address_1'] = $fields['shipping']['shipping_address_1'];
    $fields2['shipping']['shipping_city'] = $fields['shipping']['shipping_city'];
    $fields2['shipping']['shipping_postcode'] = $fields['shipping']['shipping_postcode'];
    $fields2['shipping']['shipping_country'] = $fields['shipping']['shipping_country'];
    $fields2['order']['order_comments'] =  $fields['order']['order_comments']; 
    $fields2['billing']['billing_first_name'] = array(
        'label' => __('First Name', 'woocommerce'),
        'class'     => array('form-row-wide'),
        'clear'     => true,
        'required' => true
    );
    $fields2['billing']['billing_last_name'] = array(
        'label' => __('Last Name', 'woocommerce'),
        'class'     => array('form-row-wide'),
        'clear'     => true,
            'required' => true
    );

     $fields2['shipping']['shipping_first_name'] = array(
            'label' => __('First Name', 'woocommerce'),
        'class'     => array('form-row-wide'),
        'clear'     => true,
        'required' => true
    );
    $fields2['shipping']['shipping_last_name'] = array(
        'label' => __('Last Name', 'woocommerce'),
        'class'     => array('form-row-wide'),
        'clear'     => true,
            'required' => true
    );
    return $fields2;
}

Is there a way to combine and/or simplify this? It works great, but seems so long and complicated.

1
  • Maybe you should ask this one on codereview.stackexchange.com? Commented Nov 7, 2015 at 20:27

1 Answer 1

1

How about something like this.

add_filter("woocommerce_checkout_fields", "reorder_woo_fields");

function reorder_woo_fields($fields) {

    $billing_order = array(
        "billing_first_name", 
        "billing_last_name", 
        "billing_address_1", 
        "billing_city", 
        "billing_postcode", 
        "billing_email", 
        "billing_phone", 
        "billing_country"
    );
    foreach($billing_order as $field)
    {
        $billing_fields[$field] = $fields["billing"][$field];
    }

    $shipping_order = array(
        "shipping_first_name",
        "shipping_last_name",
        "shipping_address_1",
        "shipping_city",
        "shipping_postcode",
        "shipping_country",
    );
    foreach($shipping_order as $field)
    {
        $shipping_fields[$field] = $fields["shipping"][$field];
    }


    $fields["billing"] = $billing_fields;
    $fields["shipping"] = $shipping_fields;

    return $fields;
}

EDIT:

add_filter( 'woocommerce_billing_fields', 'add_custom_css_on_checkout', 10, 1 );
function add_custom_css_on_checkout( $address_fields ) {
    $address_fields['billing_first_name']['class'] = 'form-row-wide';
    $address_fields['billing_last_name']['class'] = 'form-row-wide';
    return $address_fields;
}
Sign up to request clarification or add additional context in comments.

3 Comments

But this does not include the function to make first and last name full width...
Try the edit and add other fields as well, i think you just need the class parameter in the array.
Then just add your code at the end of the code. A better way of doing this would be to hook into the checkout fields and hook your css classes. Checkout the edit. Similarly you will have to do it for shipping address as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.