【问题标题】:Set specific display order on WooCommerce custom checkout fields在 WooCommerce 自定义结帐字段上设置特定的显示顺序
【发布时间】:2020-01-14 17:59:17
【问题描述】:

我正在向 woocommerce 结帐结算部分页面添加 20 个自定义结帐字段。它以前工作正常。但是最近我们发现字段的显示顺序已经混乱了。我希望有人可以帮助我按添加顺序显示自定义字段。

我已禁用除 woocommerce 之外的所有插件。我使用的是二十九主题。我删除了所有自定义字段,然后一次添加一个。奇怪的是,我能够添加 11 个按顺序显示的字段。当我们添加 12 个或更多字段时,显示混乱。我用一个简单测试字段的多个副本替换了所有定制的自定义字段,但问题仍然存在。

以下代码被添加到主题functions.php

add_filter( 'woocommerce_checkout_fields' , 
'custom_override_checkout_fields',10,3 ); 

function custom_override_checkout_fields( $fields ) {

//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']);

//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 name
Email address
test1
test2 
test3
....
test20

实际布局是:-

First name
test10
test19
test18
test17
test16
test15
test14
test13
test12
test11
Last name
test9
test8
test7
test6
test5
test4
test3
test2
test1

【问题讨论】:

    标签: php wordpress woocommerce checkout custom-fields


    【解决方案1】:

    您错过了表单字段“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;
    }
    

    【讨论】:

    • 非常感谢,我已经实现了优先级字段并且结帐表单正确显示。
    猜你喜欢
    • 2021-05-28
    • 2018-01-03
    • 2023-04-10
    • 2016-11-07
    • 2021-02-07
    • 2020-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多