svelandiag
svelandiag

Reputation: 4320

How to add a custom checkout field in specific location on WooCommerce

Im adding a custom address field in checkout with this code:

add_filter( 'woocommerce_checkout_fields' , 'checkout_address_details_fields' );

// Our hooked in function – $fields is passed via the filter!
function checkout_address_details_fields( $fields ) {
    $fields['shipping']['shipping_address_details'] = array(
        'label'     => __('Añade más detalles a tu dirección', 'woocommerce'),
    'placeholder'   => _x('Bloque X Apartemento XXX (Opcional)', 'placeholder', 'woocommerce'),
    'required'  => false,
    'class'     => array('form-row-wide'),
    'clear'     => true
     );

     $fields['billing']['billing_address_details'] = array(
        'label'     => __('Añade más detalles a tu dirección', 'woocommerce'),
    'placeholder'   => _x('Bloque X Apartamento XXX (Opcional)', 'placeholder', 'woocommerce'),
    'required'  => false,
    'class'     => array('form-row-wide'),
    'clear'     => true
    );

     return $fields;
}

It is adding the field correctly, however, the field is added at the end below phone field, I need to add this custom field in the 2nd place, below the Street Address field.

Upvotes: 2

Views: 2143

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253784

You need to use the "priority" argument to set the correct location of your checkout fields like:

add_filter( 'woocommerce_checkout_fields' , 'checkout_address_details_fields' );
function checkout_address_details_fields( $fields ) {
    $fields['shipping']['shipping_address_details'] = array(
        'label'       => __('Añade más detalles a tu dirección', 'woocommerce'),
        'placeholder' => _x('Bloque X Apartemento XXX (Opcional)', 'placeholder', 'woocommerce'),
        'required'    => false,
        'class'       => array('form-row-wide'),
        'clear'       => true,
        'priority'    => 55, // <===== Here
    );

    $fields['billing']['billing_address_details'] = array(
        'label'       => __('Añade más detalles a tu dirección', 'woocommerce'),
        'placeholder' => _x('Bloque X Apartamento XXX (Opcional)', 'placeholder', 'woocommerce'),
        'required'    => false,
        'class'       => array('form-row-wide'),
        'clear'       => true,
        'priority'    => 55, // <===== Here
    );

    return $fields;
}

It should work.

Upvotes: 3

Related Questions