• Hi,

    after updating to woocommerce 3.0.4, my code to reorder checkout fields is not working anymore (it worked in 3.0.3). This is the code:

    
    add_filter("woocommerce_billing_fields", "ap_ja_order_billing_fields");
    
    function ap_ja_order_billing_fields($fields) {
    
        $order = array(
            "billing_country",
            "billing_postcode", 
            "billing_state",
            "billing_address_1", 
            "billing_address_2",
            "billing_company", 
            "billing_last_name",
            "billing_first_name",
            "billing_furigana_last_name",
            "billing_furigana_first_name",
            "billing_email", 
            "billing_phone"
    
        );
        foreach($order as $field)
        {
            $ordered_fields[$field] = $fields[$field];
        }
    
        $fields = $ordered_fields;
        return $fields;
    
    }

    No matter how I change it, it appears to randomly alter things, but first and last name always stay on top. It worked perfectly until 3.0.4.

    Thank you for your help

    Alessio

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    This type of hacky code is no longer needed. Instead, set the priority of each field e.g. $fields['billing_country']['priority']. They will be sorted by that.

    Thread Starter vervelover

    (@vervelover)

    Hi,

    thank you for your answer, it looks way easier but for some reason it doesn’t work 100% for me.

    this code:

    add_filter( 'woocommerce_checkout_fields' , 'ap_ja_custom_override_checkout_fields');
    
    // Our hooked in function - $fields is passed via the filter!
    function ap_ja_custom_override_checkout_fields( $fields ) {
         $fields['billing']['billing_country']['priority'] = 1;
         $fields['billing']['billing_postcode']['priority'] = 2;
         $fields['billing']['billing_state']['priority'] = 3;
         $fields['billing']['billing_address_1']['priority'] = 4;
         $fields['billing']['billing_address_2']['priority'] = 5;
         $fields['billing']['billing_company']['priority'] = 6;
         $fields['billing']['billing_last_name']['priority'] = 7;
         $fields['billing']['billing_first_name']['priority'] = 8;
         $fields['billing']['billing_furigana_last_name']['priority'] = 9;
         $fields['billing']['billing_furigana_first_name']['priority'] = 10;
         $fields['billing']['billing_phone']['priority'] = 11;
         $fields['billing']['billing_email']['priority'] = 12;
         $fields['billing']['billing_email-2']['priority'] = 13;
         return $fields;
    }

    only seems to work for some of the fields, while others don’t. It works for country, first name, last name, my custom added fields, email, email-2 but it dosn’t work for postcode, state and address-1/address-2 as it places those at the end, except “company” at the second place right after country.

    Thank you for your help!

    Alessio

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Look at the default priorities – they increment in 10’s. 10, 20 30.. Some fields like state/postcode/country also get reordered based on locale (e.g. some counties show postcode before the city).

    Just reorder the custom ones, not all of them.

    Thread Starter vervelover

    (@vervelover)

    Hi,

    ok this way it was better, but no matter what I do I can’t put postcode and state right after country, as address would always stay in between.

    I set country=10 postcode=20 state=30, but then even if I ignore address, or set it to be like =100 or =120, it will stay between country and postcode.

    Thank you for your help,

    Alessio

    @vervelover, I posted an updated snippet for you and all the community here: https://businessbloomer.com/woocommerce-move-reorder-fields-checkout-page/

    Hope this helps ??

    Rodolfo

    Thread Starter vervelover

    (@vervelover)

    @rodolfo,

    Thank you, that was great help! I now managed to achieve what I needed, I’ll post the code for the sake of it:

    add_filter( 'woocommerce_default_address_fields', 'bbloomer_move_checkout_fields_woo_3' );
      
    function bbloomer_move_checkout_fields_woo_3( $fields ) {
     
      $fields['address_1']['priority'] = 71;
      $fields['address_2']['priority'] = 72;
      $fields['country']['priority'] = 7;
      $fields['postcode']['priority'] = 8;
      $fields['state']['priority'] = 9;
      $fields['company']['priority'] = 73;
      $fields['last_name']['priority'] = 74;
      $fields['first_name']['priority'] = 75;
       
      return $fields;
    }

    One last question, where can I find the default field priorities?

    Thanks again for your help,

    Alessio

    Hi @mikejolley,

    Based on my locale (Netherlands) postcode comes before city. That’s working fine. But when I unset some of the fields, then postcode automaticly flips back after city. And I don’t understand why. This is the unset code I used:

    add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
    
    function custom_override_checkout_fields( $fields ) {
       unset( $fields["billing"]["billing_phone"] );
       unset( $fields["billing"]["billing_country"] );
       unset( $fields["billing"]["billing_address_2"] );  
       unset( $fields["shipping"]["shipping_country"] );
       unset( $fields["shipping"]["shipping_address_2"] );
       return $fields;
    }

    Why is this going wrong?

    • This reply was modified 7 years, 7 months ago by Scholto Bos.
    • This reply was modified 7 years, 7 months ago by Scholto Bos.

    Could please someone tell me what is the benefit of having a new system of priorities if:
    – they are not always respected (element with priority 55 can go before 35)
    – ther’s not 1 but 3 functions that modify fields array and, after about 7 hours of tries, it seems that all of them get their data overwritten at some point
    – there’s no longer an easy way to modify fields as it used to be.

    Let me explain.

    1. Hooking up to “woocommerce_checkout_fields” and changing the order in array doesn’t work any longer as the array doesn’t seem to modify anything anymore + new, empty fields seem to popup when the fn is used
    2. woocommerce_billing_fields filter doesn’t seem to fully work either since using this array:

    $fields[‘billing_email’][‘priority’] = 1;
    $fields[‘billing_first_name’][‘priority’] = 10;
    $fields[‘billing_last_name’][‘priority’] = 15;
    $fields[‘billing_company’][‘priority’] = 20;
    $fields[‘billing_address_1’][‘priority’] = 25;
    $fields[‘billing_address_2’][‘priority’] = 30;
    $fields[‘billing_postcode’][‘priority’] = 35;
    $fields[‘billing_city’][‘priority’] = 40;
    $fields[‘billing_state’][‘priority’] = 45;
    $fields[‘billing_country’][‘priority’] = 50;
    $fields[‘billing_phone’][‘priority’] = 55;

    Seems to create incorrect output where phone with priority 55 gets positioned (i suppose by JS) between 25 and 30

    3. Using the above mentioned filter it’s possible to remove focus from name but one cannot add it to email

    4. using “woocommerce_default_address_fields” filter does not work on email or phone.

    I cannot utter how increadibly bad it is and how much of a problem it has now become.

    So please, if anyone really cares to make it work, please do.

    @chrisplaneta

    @scholtobos

    Reordering fields/setting priorities >= WC3.0.4 does indeed work and better than it used to! The below comment should provide the answers you’re looking for…

    https://github.com/woocommerce/woocommerce/issues/14618#issuecomment-310248919

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘reordering checkout fields doesn’t work anymore on 3.0.4’ is closed to new replies.