$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>filters - How to reorder billing fields in WooCommerce Checkout template?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

filters - How to reorder billing fields in WooCommerce Checkout template?

matteradmin8PV0评论
Closed. This question is off-topic. It is not currently accepting answers.

Questions should be specific to WordPress within defined scope (merely happening in its context, such as generic PHP/JS/HTML/CSS, is insufficient). Might be better asked at Stack Overflow or other appropriate site of Stack Exchange network.

Closed 11 years ago.

Improve this question

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.phpare 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?

Closed. This question is off-topic. It is not currently accepting answers.

Questions should be specific to WordPress within defined scope (merely happening in its context, such as generic PHP/JS/HTML/CSS, is insufficient). Might be better asked at Stack Overflow or other appropriate site of Stack Exchange network.

Closed 11 years ago.

Improve this question

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.phpare 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?

Share Improve this question asked Jan 5, 2013 at 16:45 MTTMTT 3,59612 gold badges47 silver badges74 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 31

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;

}

Thanks to Dbranes for the answer.

Replace:

<?php foreach ($checkout->checkout_fields['billing'] as $key => $field) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>

With:

<?php 
// order the keys for your custom ordering or delete the ones you don't need
$mybillingfields=array(
    "billing_first_name",
    "billing_last_name",
    "billing_company",
    "billing_address_1",
    "billing_address_2",
    "billing_city",
    "billing_state",
    "billing_postcode",
    "billing_country",
    "billing_email",
    "billing_phone",
);
foreach ($mybillingfields as $key) : ?>
<?php woocommerce_form_field( $key, $checkout->checkout_fields['billing'][$key], $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>

You can make a copy into your theme and edit the template that renders the checkout form.

Adapted from the plugin documentation:

Example
To overide the admin order notification, copy: woocommerce/templates/checkout/form-checkout.php
to
yourtheme/woocommerce/checkout/form-checkout.php

[update]

In this file, just before the fields being printed, there's this action hook: do_action('woocommerce_before_checkout_billing_form', $checkout);.

So, it's just a matter of adding this action in the theme's functions.php or in a custom plugin and reordering the fields as the OP shows in his Answer. No need of overriding the template, or yes if further customizations are needed.

Post a comment

comment list (0)

  1. No comments so far