Position the zip code field at the Woocommerce checkout

Asked

Viewed 724 times

2

I need to position the zip code field before the address because I will load the address using the post office api, I can position the other fields, but the "billing_postcode" does not obey the order, which can be?

add_filter("woocommerce_checkout_fields", "order_fields", 30);
function order_fields($fields) {
    $fields["billing"]["billing_cpf_cli"]["priority"] = 8;
    $fields["billing"]["billing_rg_cli"]["priority"] = 9;
    $fields["billing"]["billing_postcode"]["priority"] = 10;
    $fields["billing"]["billing_address_1"]["priority"] = 11;
    $fields["billing"]["billing_num_cli"]["priority"] = 20;
    $fields["billing"]["billing_bairro_cli"]["priority"] = 21;  



    return $fields;
}       


add_filter('woocommerce_billing_fields', 'custom_woocommerce_billing_fields');

2 answers

0

Your function is wrong, the correct is:

add_filter( 'woocommerce_checkout_fields', 'woocommerce_checkout_fields_order' );
function woocommerce_checkout_fields_order( $fields ) {
    $fields['billing']['billing_cpf_cli']['priority'] = 8;
    $fields['billing']['billing_rg_cli']['priority'] = 9;
    $fields['billing']['billing_postcode']['priority'] = 10;
    $fields['billing']['billing_address_1']['priority'] = 11;
    $fields['billing']['billing_num_cli']['priority'] = 12;
    $fields['billing']['billing_bairro_cli']['priority'] = 13;
    return $fields;
}

Woocommerce does not recognize this function that called function order_fields the correct is woocommerce_checkout_fields. Do it and see if it works... The ideal would also be to reorganize all fields because thus Oce would define from preiority 1 to infinity.

0

A friendlier way of dealing with this type of problem related to the priority of the fields is by evaluating through the inspecter of your browser elements the priority in which the current fields are, so you can orient yourself more quickly. In the element inspecter, look for data-priority="NÚMERO DA PRIORIDADE DO CAMPO"

On a standard Woocommerce checkout page address_1 has priority 50, so just put the postcode in 50 and the address_1 in 55, for example. From what I noticed in your code, you are trying to change the priority through the woocommerce_checkout_fields, but in this case it is the woocommerce_default_address_fields that you need to modify the priority, then a solution to your problem would be:

add_filter( 'woocommerce_default_address_fields', 'woocommerce_default_address_fields_reorder' );

function woocommerce_default_address_fields_reorder( $fields ) {
    $fields['postcode']['priority'] = 50; // antes era o address_1 que ocupava esta prioridade baseado no padrão WooCommerce de prioridades.
    $fields['address_1']['priority'] = 55;

    return $fields;
}

I recommend you read it yourself woocommerce documentation to better understand what it’s all about.

I hope I’ve helped,

hugs!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.