WordPress:在functions.php中运行多个数组以添加自定义字段

时间:2019-06-04 02:15:13

标签: php wordpress function woocommerce custom-fields

我正在尝试将自定义字段添加到我的WooCommerce结帐方案中(尽管在任何地方都可能存在自定义字段)。我想添加两个,但是每次尝试运行功能的尝试,它只会添加第一个自定义字段,而不添加第二个。在我的代码中哪里需要清理?

function dv_add_checkout_fields( $fields ) {
    $fields['billing_age'] = array(
         'label'        => __( 'Age of the kid(s) you mainly do activities with?' ),
         'type'        => 'select',
         'class'        => array( 'form-row-wide' ),
         'priority'     => 150,
         'required'     => true,
         'options'      => array( '0-2', '3-5', '6-8', '9-12' ),
    );
    array(
         'label'        => __( 'Which part of LA do you live in or are closest to?' ),
         'type'        => 'select',
         'class'        => array( 'form-row-wide' ),
         'priority'     => 150,
         'required'     => true,
         'options'      => array( 'Central LA', 'East LA', 'San Fernando Valley', 'San Gabriel Valley', 'South Bay', 'West LA', 'Orange County' ),
     );
     return $fields;
}
add_filter( 'woocommerce_billing_fields', 'dv_add_checkout_fields' );

1 个答案:

答案 0 :(得分:0)

实际上,您的数组语法错误,会返回数据$fields

您将返回$fields,并且仅将一个数组分配给该$fields。另一个数组未分配给$fields。请尝试下面的代码,并比较您的代码以找出区别。

function dv_add_checkout_fields( $fields ) {
    $fields['billing_age'] = array(
         'label'        => __( 'Age of the kid(s) you mainly do activities with?' ),
         'type'        => 'select',
         'class'        => array( 'form-row-wide' ),
         'priority'     => 150,
         'required'     => true,
         'options'      => array( '0-2', '3-5', '6-8', '9-12' ),
    );
    $fields['living_near'] = array(
         'label'        => __( 'Which part of LA do you live in or are closest to?' ),
         'type'        => 'select',
         'class'        => array( 'form-row-wide' ),
         'priority'     => 150,
         'required'     => true,
         'options'      => array( 'Central LA', 'East LA', 'San Fernando Valley', 'San Gabriel Valley', 'South Bay', 'West LA', 'Orange County' ),
     );
     return $fields;
}
add_filter( 'woocommerce_billing_fields', 'dv_add_checkout_fields' );