Skip to content
Advertisement

Sorting priority for specific Woocommerce checkout fields depending on country

In Woocommerce, I need to set the default ordering priority (sorting) of three fields on the checkout page:

The first change depends on the choice of the 'IT' country code:

  • postcode – ‘priority’ => 91,

The other two are free of conditions, I just need to set the default priority:

  • billing_email – ‘priority’ => 30,
  • billing_phone – ‘priority’ => 40,

By editing the file directly, class-wc-countries.php core file as shown below, I get what I need. But clearly at each update of the plugin I lose the changes.

Now I’m looking for a hook to get this done in a clean way.

In class-wc-countries.php (around line 935) from:

IT' => array(
    'postcode' => array(
        'priority' => 65, // <============= HERE
    ),
    'state'    => array(
    'required' => true,
    'label'    => __( 'Province', 'woocommerce' ),
    ),
),

I change the ‘priority’ value from 65 to 91:

IT' => array(
    'postcode' => array(
        'priority' => 91, // <============= HERE
    ),
    'state'    => array(
    'required' => true,
    'label'    => __( 'Province', 'woocommerce' ),
    ),
),

And arround line 1203 in:

  // Add email and phone fields.
  if ( 'billing_' === $type ) {
      $address_fields['billing_phone'] = array(
          'label'        => __( 'Phone', 'woocommerce' ),
          'required'     => true,
          'type'         => 'tel',
          'class'        => array( 'form-row-wide' ),
          'validate'     => array( 'phone' ),
          'autocomplete' => 'tel',
          'priority'     => 100, // <============= HERE
      );
      $address_fields['billing_email'] = array(
          'label'        => __( 'Email address', 'woocommerce' ),
          'required'     => true,
          'type'         => 'email',
          'class'        => array( 'form-row-wide' ),
          'validate'     => array( 'email' ),
          'autocomplete'=>'no'===get_option
              ('woocommerce_registration_generate_username' )
              ? 'email' : 'email username',
          'priority'     => 110, // <============= HERE
       );
    }

I change the ‘priority’ values to:

  // Add email and phone fields.
  if ( 'billing_' === $type ) {
      $address_fields['billing_phone'] = array(
          'label'        => __( 'Phone', 'woocommerce' ),
          'required'     => true,
          'type'         => 'tel',
          'class'        => array( 'form-row-wide' ),
          'validate'     => array( 'phone' ),
          'autocomplete' => 'tel',
          'priority'     => 40, // <============= HERE
      );
      $address_fields['billing_email'] = array(
          'label'        => __( 'Email address', 'woocommerce' ),
          'required'     => true,
          'type'         => 'email',
          'class'        => array( 'form-row-wide' ),
          'validate'     => array( 'email' ),
          'autocomplete'=>'no'===get_option
              ('woocommerce_registration_generate_username' )
              ? 'email' : 'email username',
          'priority'     => 30, // <============= HERE
       );
    }

Any help is appreciated.

Advertisement

Answer

Using available filter hook you can make that change without overriding core files…

The first change is made with this hook (for “IT” country code):

add_filter('woocommerce_get_country_locale', 'custom__country_locale', 30, 1 );
function wps_select_order_meta_keys( $locale ) {
    $locale['IT']['postcode']['priority'] = 91;
    return $locale;
}

For the second change you need also to change the class. It’s the same code that you already have in the answer to your other question, but without this line to be removed:

if( ! is_account_page() ) return $fields;

The code is:

add_filter(  'woocommerce_billing_fields', 'custom_billing_fields', 20, 1 );
function custom_billing_fields( $fields ) {

    ## ---- 1.  Sort billing email and phone fields ---- ##

    $fields['billing_email']['priority'] = 30;
    $fields['billing_email']['class'] = array('form-row-first');
    $fields['billing_phone']['priority'] = 40;
    $fields['billing_phone']['class'] = array('form-row-last');

    return $fields;
}

So if you remove if( ! is_account_page() ) return $fields; from the code in my other answer, it will work also on checkout page (as as already indicated)…

So you can use (for both account and checkout pages):

add_filter(  'woocommerce_default_address_fields', 'custom_default_address_fields', 20, 1 );
function custom_default_address_fields( $fields ) {

    ## ---- 1.  Remove 'address_2' field ---- ##

    unset($fields['address_2']);

    ## ---- 2.  Sort Address fields ---- ##

    // Set the order (sorting fields) in the array below
    $sorted_fields = array('first_name','last_name','company','address_1','country','postcode','city','state');

    $new_fields = array();
    $priority = 0;

    // Reordering billing and shipping fields
    foreach($sorted_fields as $key_field){
        $priority += 10;

        if( $key_field == 'company' )
            $priority += 20; // keep space for email and phone fields

        $new_fields[$key_field] = $fields[$key_field];
        $new_fields[$key_field]['priority'] = $priority;
    }
    return $new_fields;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement