Carlos Roman
Carlos Roman

Reputation: 81

Woocommerce change field position in checkout page

I'm creating a custom field for billing section in checkout page, i see in source a data-priority attribute, but i don't know how to move my field to after Primary Name & Last Name

My billing form

Billing form

The code displayed on Chrome Dev Tools

Code

In simple words, I need to move the field to top.

Upvotes: 3

Views: 10025

Answers (3)

Jefferson Maes
Jefferson Maes

Reputation: 21

Try this:

    function custom_field( $fields ) {
       $fields['billing']['RUT'] = array(
           'priority'   => 20 //or the number on position you what
            );
       return $fields;
    }
    add_filter( 'woocommerce_checkout_fields' , 'custom_field');

Upvotes: 1

Mizanur Rahman Khan
Mizanur Rahman Khan

Reputation: 1851

Use procedure that are below. For woocommerce greter then version 3

add_filter( 'woocommerce_default_address_fields', 'mrks_woocommerce_default_address_fields' );

function mrks_woocommerce_default_address_fields( $fields ) {

    // default priorities: 
    // 'first_name' - 10
    // 'last_name' - 20
    // 'company' - 30
    // 'country' - 40
    // 'address_1' - 50
    // 'address_2' - 60
    // 'city' - 70
    // 'state' - 80
    // 'postcode' - 90

  // e.g. move 'company' above 'first_name':
  // just assign priority less than 10
  $fields['state']['priority'] = 41;

  return $fields;
}

For woocommerce less then version 3

add_filter( 'woocommerce_checkout_fields', 'mrks_move_checkout_fields' );

function mrks_move_checkout_fields( $fields ) {

  // Billing: move these around in the order you'd like

  $fields2['billing']['billing_first_name'] = $fields['billing']['billing_first_name'];
  $fields2['billing']['billing_last_name']  = $fields['billing']['billing_last_name'];
  $fields2['billing']['billing_company']    = $fields['billing']['billing_company'];
  $fields2['billing']['billing_address_1']  = $fields['billing']['billing_address_1'];
  $fields2['billing']['billing_address_2']  = $fields['billing']['billing_address_2'];
  $fields2['billing']['billing_city']       = $fields['billing']['billing_city'];
  $fields2['billing']['billing_postcode']   = $fields['billing']['billing_postcode'];
  $fields2['billing']['billing_state']      = $fields['billing']['billing_state'];
  $fields2['billing']['billing_country']    = $fields['billing']['billing_country'];
  $fields2['billing']['billing_email']      = $fields['billing']['billing_email'];
  $fields2['billing']['billing_phone']      = $fields['billing']['billing_phone'];

  // Shipping: move these around in the order you'd like

  $fields2['shipping']['shipping_first_name'] = $fields['shipping']['shipping_first_name'];
  $fields2['shipping']['shipping_last_name']  = $fields['shipping']['shipping_last_name'];
  $fields2['shipping']['shipping_company']    = $fields['shipping']['shipping_company'];
  $fields2['shipping']['shipping_address_1']  = $fields['shipping']['shipping_address_1'];
  $fields2['shipping']['shipping_address_2']  = $fields['shipping']['shipping_address_2'];
  $fields2['shipping']['shipping_city']       = $fields['shipping']['shipping_city'];
  $fields2['shipping']['shipping_postcode']   = $fields['shipping']['shipping_postcode'];
  $fields2['shipping']['shipping_state']      = $fields['shipping']['shipping_state'];
  $fields2['shipping']['shipping_country']    = $fields['shipping']['shipping_country'];

  $checkout_fields = array_merge( $fields, $fields2);
  return $checkout_fields;
}

Upvotes: 6

Tazir
Tazir

Reputation: 91

WooCommerce added priority without much documentation (just from WC 3). You need to add to functions.php

add_filter("woocommerce_checkout_fields", "order_fields", 30);
function order_fields($fields) {
    $fields["billing"]["user_rut"]["priority"] = 30;
    return $fields;
}

You might also need to use the old order method [Maybe just priority without reordering the fields not working]

Upvotes: 3

Related Questions