3
votes

Thanks to the filter "WooCommerce admin billing fields" I have ordered the billing fields in the notificiación footer by email but when I try to insert my custom billing field does not appear.

add_filter( 'woocommerce_order_formatted_billing_address' , 'woo_reorder_billing_fields', 10, 2 );

function woo_reorder_billing_fields( $address, $wc_order ) {

    $address = array(
        'first_name'    => $wc_order->billing_first_name,
        'last_name'     => $wc_order->billing_last_name,
        'company'       => $wc_order->billing_company,
        'my_field'      => $wc_order->billing_my_field,
        'country'       => $wc_order->billing_country
        );

    return $address;
}

In order admin edit I can show my custom billing field thanks to the filter "woocommerce_admin_billing_fields", first adding the field and then reordering the Array.

I note that I added before and I have reordered this field in my checkout with the filter "woocommerce_checkout_fields".

Why not show my custom field if "$ wc_order" object stores the field in the checkout?

Any ideas?

Thanks in advance!

1
I found the solution, its in the "class-wc-countries", that format de billing address. I write the solution soonJosé María Lamela Real
check this link may be it's your question solution :wordpress.stackexchange.com/questions/120347/…R.K.Bhardwaj
@RonnieEsponja: how have you resolved?Sefran2
Answer below, Sefran2.José María Lamela Real

1 Answers

6
votes

Yes, here the explanation:

We will register the new cusotm field, in this example the field "VAT" to store the fiscal document for companies in the European Union. There is a lot of documentation on how to register the custom fields and display them in the admin / user panel.

add_filter( 'woocommerce_default_address_fields', 'woo_new_default_address_fields' );

function woo_new_default_address_fields( $fields ) {

    $fields['vat'] = array(

        'label' => __( 'VAT number', 'woocommerce' ),
        'class' => array( 'form-row-wide', 'update_totals_on_change' ),

    );
    
    return $fields;

}

Then add the new field "VAT" only to the registration of billing fields for mails, we do not want it to appear in the address fileds section.

add_filter( 'woocommerce_order_formatted_billing_address' , 'woo_custom_order_formatted_billing_address', 10, 2 );

function woo_custom_order_formatted_billing_address( $address, $WC_Order ) {
    
    $address = array(
        'first_name'    => $WC_Order->billing_first_name,
        'last_name'     => $WC_Order->billing_last_name,
        'vat'           => $WC_Order->billing_vat,
        'company'       => $WC_Order->billing_company,
        'address_1'     => $WC_Order->billing_address_1,
        'address_2'     => $WC_Order->billing_address_2,
        'city'          => $WC_Order->billing_city,
        'state'         => $WC_Order->billing_state,
        'postcode'      => $WC_Order->billing_postcode,
        'country'       => $WC_Order->billing_country
        );
    
    return $address;

}

The following code customizes the appearance of the addresses, this is where we must add the call to the new VAT field. This allows us to customize the address view for each country independently.

add_filter( 'woocommerce_formatted_address_replacements', function( $replacements, $args ){
    
    $replacements['{vat}'] = $args['vat'];
    return $replacements;

}, 10, 2 );

add_filter( 'woocommerce_localisation_address_formats' , 'woo_includes_address_formats', 10, 1);

function woo_includes_address_formats($address_formats) {

    $address_formats['ES'] = "{name}\n{company}\n{vat}\n{address_1}\n{address_2}\n{postcode} {city}\n{state}\n{country}";
    $address_formats['default'] = "{name}\n{company}\n{vat}\n{nif}\n{address_1}\n{address_2}\n{city}\n{state}\n{postcode}\n{country}";
    
    return $address_formats;

}

Any questions ask!