问题描述

我正在使用 WooTheme 的 Customizing checkout fields using actions and filters 创建一个 madlib 样式的结帐表单。

结帐模板 form-billing.php 中的结算字段将显示在此调用中:

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

如何改变字段出现的顺序?

当前 (默认) 字段顺序是:名字姓氏公司 (为我隐藏) 城市/城市邮政编码国家电子邮件电话

默认顺序:

我希望这些领域对于美国人 (我所在的地方) 来说是一个更自然的秩序,所以:名字姓氏公司 (对我来说是隐藏的) 城市/城市州邮政编码国家电子邮件电话

我该怎么做最好呢?

最佳解决方案

您可以通过 functions.php(您的 (子)) 主题完成相同的操作:

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;

}

次佳解决方案

感谢 Dbranes 的答案。

更换:

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

附:

<?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; ?>

参考文献

注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。