問題描述

我正在使用 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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。