I’m creating a madlib style checkout form using WooTheme’s Customizing checkout fields using actions and filters.
Billing fields in the checkout template form-billing.php
are displayed with this call:
<?php foreach ($checkout->checkout_fields['billing'] as $key => $field) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>
How can change the order the fields appear?
The current (default) field order is:
first name
last name
company (hidden for me)
town/city
zipcode
country
state
email
phone
Default order:
I want the fields to be in a more natural order for Americans (where I live), so:
first name
last name
company (hidden for me)
town/city
state
zipcode
country
email
phone
How can I best do this?
Same can be done through functions.php
in your (child) theme:
add_filter("woocommerce_checkout_fields", "order_fields");
function order_fields($fields) {
$order = array(
"billing_first_name",
"billing_last_name",
"billing_company",
"billing_address_1",
"billing_address_2",
"billing_postcode",
"billing_country",
"billing_email",
"billing_phone"
);
foreach($order as $field)
{
$ordered_fields[$field] = $fields["billing"][$field];
}
$fields["billing"] = $ordered_fields;
return $fields;
}