Search code examples
phpwordpresswoocommercecheckoutcustom-taxonomy

Change the order from additional checkout fields in WooCommerce checkout manager


In Woocommerce, I would like to change additional checkout fields order (programmatically) made with WooCommerce checkout manager plugin. This is what I have actually:

enter image description here

I will need to change the order from 'Rolls per Carton' field after 'Format' field.

Any help is appreciated.


Solution

  • WooCommerce checkout manager plugin doesn't allow to reorder additional fields and it save that data in some of his option settings.

    So the way is to alter the plugin settings just once.

    To run the following code, just browse any page of your web site.
    You will run it just once and then remove it.

    The code:

    function wccs_change_fields_order(){
        $wccs_settings = get_option( 'wccs_settings' );
    
        // Get the array of additional fields
        $additional_fields = $wccs_settings['buttons'];
    
        // SETTINGS: Here set your 2 product attribute Names
        $format    = 'Format';
        $rp_carton = 'Rolls per Carton';
    
        // FIRST Loop Get the array key for 'Rolls per Carton' field label
        foreach( $additional_fields as $key => $field ){
            if( $field['label'] == $rp_carton )
                $key_rp_carton = $key;
        }
    
        // Save 'Rolls per Carton' data field in a variable
        $field_rp_carton = $additional_fields[$key_rp_carton];
    
        // Remove 'Rolls per Carton' data field from the fields array
        unset($additional_fields[$key_rp_carton]);
    
        // Initializing variables
        $new_additional_fields = array();
        $sorting_index = 1;
    
        // SECOND Loop reordering fields
        foreach( $additional_fields as $key => $field ){
            // Update "order" argument
            $field['order'] = $sorting_index;
    
            // Add the current field to a new array
            $new_additional_fields[] = $field;
    
            $sorting_index++; // Increment sorting index
    
            // When we reach 'Format' data field
            if( $field['label'] == $format ){
                // Set a new position to 'Rolls per Carton' attribute
                $field_rp_carton['order'] = $sorting_index;
    
                // Add 'Rolls per Carton' field data in the new array
                $new_additional_fields[] = $field_rp_carton;
    
                $sorting_index++; // Increment sorting index
            }
        }
        // Set back the reordered additional fields in the main array
        $wccs_settings['buttons'] = $new_additional_fields;
    
        // Update the new changed options (Save)
        update_option( 'wccs_settings', $wccs_settings, 'yes' );
    }
    
    wccs_change_fields_order();
    

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