WordPress: Running Multiple Arrays in functions.php to add custom fields

911

I'm trying to add custom fields to my WooCommerce checkout scheme (although this could be a situation with custom fields anywhere). I want to add two, but each attempt I've tried to run a function, it only adds the first custom field and not the second. Where in my code does this need to be cleaned up?

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' );
292

Answer

Solution:

Actually your array syntax is wrong which return the data$fields.

you are returning$fields and only one array is assigned to that$fields. Another array was not assigned to$fields . Please try below code and compare your code to find the difference.

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

People are also looking for solutions to the problem: php - Set of Excises arrays, vectors and more array Pt 1

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.