亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 WooCommerce 自定義結帳字段上設置特定的顯示順序

在 WooCommerce 自定義結帳字段上設置特定的顯示順序

PHP
千萬里不及你 2022-01-08 17:43:15
我正在向 woocommerce 結帳計費部分頁面添加 20 個自定義結帳字段。它以前工作正常。但是最近我們發現字段的顯示順序已經混亂了。我希望有人可以幫助我按照添加的順序顯示自定義字段。我已禁用除 woocommerce 之外的所有插件。我使用的是二十九主題。我刪除了所有自定義字段,然后一次添加一個。奇怪的是,我能夠添加按順序顯示的 11 個字段。當我們添加 12 個或更多字段時,顯示混亂。我用一個簡單測試字段的多個副本替換了所有定制的自定義字段,但問題仍然存在。以下代碼已添加到主題 functions.phpadd_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields',10,3 ); function custom_override_checkout_fields( $fields ) {//unset the unwanted billing fieldsunset($fields['order']['order_comments']);unset($fields['billing']['billing_company']);unset($fields['billing']['billing_address_1']);unset($fields['billing']['billing_address_2']);unset($fields['billing']['billing_city']);unset($fields['billing']['billing_postcode']);unset($fields['billing']['billing_state']);unset($fields['billing']['billing_phone']);//add custom fields$fields['billing']['billing_test1'] = array(    'label'       => __('test1', 'woocommerce'),    'placeholder' => _x('', 'placeholder', 'woocommerce'),    'required'    => true,    'clear'       => true,    'class'     => array('form-row'),    );$fields['billing']['billing_test2'] = array(    'label'       => __('test2', 'woocommerce'),    'placeholder' => _x('', 'placeholder', 'woocommerce'),    'required'    => true,    'clear'       => true,    'class'     => array('form-row'),    );//a further 18 copies of the above field test3->test20 return $fields;}布局應該是:-First name    Last nameEmail addresstest1test2 test3....test20實際布局是:-First nametest10test19test18test17test16test15test14test13test12test11Last nametest9test8test7test6test5test4test3test2test1
查看完整描述

1 回答

?
郎朗坤

TA貢獻1921條經驗 獲得超9個贊

您錯過了priority允許重新排序表單字段的表單字段“ ”參數……在下面的代碼中,我使用for循環動態生成 20 字段(僅用于測試,因為它是最快的)。


在這里,這些表單字段的優先級從第一個的 200 開始,每個字段增加 10。


代碼:


add_filter( 'woocommerce_checkout_fields', 'customizing_checkout_fields', 10, 1 );

function customizing_checkout_fields( $fields ) {


    ## 1. unset the unwanted billing fields


    unset($fields['order']['order_comments']);

    unset($fields['billing']['billing_company']);

    unset($fields['billing']['billing_address_1']);

    unset($fields['billing']['billing_address_2']);

    unset($fields['billing']['billing_city']);

    unset($fields['billing']['billing_postcode']);

    unset($fields['billing']['billing_state']);

    unset($fields['billing']['billing_phone']);


    ## 2. Add 20 form fields (from "Test 1" to "Test 20")


    // Using a for loop to make the 20 fields dynamically

    for ( $i = 1, $j = 0; $i <= 20; $i++, $j += 10 ) {


        $fields['billing']['billing_test' . $i] = array(

            'label'       => __('Test', 'woocommerce') . ' ' . $i,

            'placeholder' => _x('', 'placeholder', 'woocommerce'),

            'required'    => true,

            'clear'       => true,

            'class'       => array('form-row'),

            'priority'    => (200 + $j) // <== The priority starting at 200 and increasing by 10 each time

        );

    }


    return $fields;

}

代碼位于您的活動子主題(或活動主題)的 function.php 文件中。測試和工作。


因此,在您的情況下,您將使用(沒有 for 循環):


add_filter( 'woocommerce_checkout_fields', 'customizing_checkout_fields', 10, 1 );

function customizing_checkout_fields( $fields ) {


    ## 1. unset the unwanted billing fields


    unset($fields['order']['order_comments']);

    unset($fields['billing']['billing_company']);

    unset($fields['billing']['billing_address_1']);

    unset($fields['billing']['billing_address_2']);

    unset($fields['billing']['billing_city']);

    unset($fields['billing']['billing_postcode']);

    unset($fields['billing']['billing_state']);

    unset($fields['billing']['billing_phone']);


    ## 2. Add 20 form fields (from "Test 1" to "Test 20")


    $fields['billing']['billing_test1'] = array(

        'label'       => __('Test 1', 'woocommerce'),

        'placeholder' => _x('', 'placeholder', 'woocommerce'),

        'required'    => true,

        'clear'       => true,

        'class'       => array('form-row'),

        'priority'    => 200 // <== <== <== priority

    );


    $fields['billing']['billing_test2'] = array(

        'label'       => __('Test 2', 'woocommerce'),

        'placeholder' => _x('', 'placeholder', 'woocommerce'),

        'required'    => true,

        'clear'       => true,

        'class'       => array('form-row'),

        'priority'    => 210 // <== Increased by 10

    );


    // A further 18 copies of the above field from "Test 3" to "Test 20"


    return $fields;

}


查看完整回答
反對 回復 2022-01-08
  • 1 回答
  • 0 關注
  • 139 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號