【问题标题】:WordPress: Running Multiple Arrays in functions.php to add custom fieldsWordPress:在 functions.php 中运行多个数组以添加自定义字段
【发布时间】:2019-10-19 13:12:52
【问题描述】:

我正在尝试将自定义字段添加到我的 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' );

【问题讨论】:

    标签: php wordpress function woocommerce custom-fields


    【解决方案1】:

    实际上你的数组语法错误,返回数据$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' );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-13
      • 1970-01-01
      相关资源
      最近更新 更多