Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • rlautan

    (@rlautan)

    Just found out I made a booboo. Use uasort instead of usort to keep the associative array.

    rlautan

    (@rlautan)

    For everyone struggling with the order of the fields here is something that might help. The checkout fields get passed through a filter called ‘woocommerce_checkout_fields’. I used the filter to order the fields based on their priority, but there are more possibilities using this method:

    
    // Filter will do its magic before the fields are passed to the template.
    add_filter('woocommerce_checkout_fields', function($fields) {
    
    	// Do things with your fields like setting the
    	// priority, label, required, etc.
    	// or removing them
    
    	// Example: Set the priorities straight
    	// Probably not meant for this since priority is something
    	// totally different than order.
    	$fields['billing']['billing_first_name']['priority'] = 0;
    	$fields['billing']['billing_last_name']['priority'] = 5;
    	$fields['billing']['billing_email']['priority'] = 10;
    	$fields['billing']['billing_phone']['priority'] = 15;
    	$fields['billing']['billing_country']['priority'] = 20;
    	$fields['billing']['billing_state']['priority'] = 25;
    	$fields['billing']['billing_address_1']['priority'] = 30;
    	$fields['billing']['billing_address_2']['priority'] = 35;
    	$fields['billing']['billing_postcode']['priority'] = 40;
    	$fields['billing']['billing_city']['priority'] = 45;
    
    	// Sort the fields based on their 'priority'
    	usort($fields['billing'], function($a, $b) {
    		return $a['priority'] <=> $b['priority'];
    	});
    
    	// Send the fields to the function that receives the fields data
    	return $fields;
    }
    

    This works great without having to edit the template at all.

    Conclusion: Just setting the priority does NOT completely change the order. Some Woocommerce sorting problem (or intention) might be in works here.

    • This reply was modified 8 years ago by rlautan. Reason: grammar
Viewing 2 replies - 1 through 2 (of 2 total)