NO BUG: Cannot change the order of fields in woocommerce checkout

It looks like bricks is doing its own rendering of checkout fields, so I can’t influence them through the code. Specifically, I want to change the location of the fields, their order. To do this, I change the priority of the fields, but in the code I see data-priority, which does not change in any way

add_filter( 'woocommerce_checkout_fields', 'tix_del_fields',100000000 );
 
function tix_del_fields( $fields ) {
 

	unset( $fields[ 'billing' ][ 'billing_company' ] );
	unset( $fields[ 'billing' ][ 'billing_postcode' ] ); 
	$array['billing']['billing_state']['priority'] = 1000;
	$array['billing']['billing_state']['data-priority'] = 1000;

 	$fields['billing']['billing_state']['label'] = 'Город';
	return $fields;
 
}

No matter how I edit the priorities for the filter function, nothing helps

Hi Oleg,
Thanks so much for your report!

The WooCommerce fields are divided into groups, each with its own priority. Details can be found in this blog post:

If I stick to the given priorities, I can change the order of the fields without issues.

Best regards,
timmse

Yes, I’ve tried this solution, it doesn’t work

Ok, WooCommerce seems to have different rules for the “state” field :smiley:
For some fields, the way described in the article works - for others, it won’t (which is also mentioned).

However, I think your code would not work in a standard theme either - you can test it when you can.

Please try this:

<?php 
add_filter( 'woocommerce_default_address_fields', 'custom_override_default_locale_fields' );
function custom_override_default_locale_fields( $fields ) {
    $fields['state']['priority'] = 3;
    return $fields;
}
?>

I tried this code and it didn’t work, the priority is also 80

Have you tried it with a standard theme? If it doesn’t work there either, it’s not a Bricks bug.

Hello @olegtix!

Bricks has nothing to do with that specific problem. It is caused by a JS file on client-side reordering the fields depending on the locale/country.

Here’s a snippet that should fix your problem :

<?php 
add_filter( 'wc_address_i18n_params', function($params) {
	$locales = json_decode($params['locale'], true);
	foreach($locales as &$locale) {
		if (isset($locale['postcode'])) {
			$locale['postcode']['priority'] = 55;
		}
	}
	$params['locale'] = json_encode($locales);
	return $params;
});
?>

Just in case someone needs more details : this snippet should go in Bricks child theme’s functions.php file.

Moreover, after some research, I was able to retrieve the original GitHub post where I found the solution last year, so all credits to that person.

Have a nice day!

@timmse :wave: