Reputation: 503
I'm trying to add an additional field in woocommerce billing part. I want "title" field to be displayed before the "name" field.
I have tried this:
// Add a new checkout field
function custom_filter_checkout_fields($fields){
$fields['billing_title_field'] = array(
'some_field' => array(
'type' => 'text',
'required' => true,
'label' => __( 'Some field' )
)
);
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'custom_filter_checkout_fields' );
and then
function order_fields($fields) {
$order = array(
"billing_title_field",
"billing_first_name",
"billing_last_name",
"billing_email",
"billing_phone",
"billing_country",
"billing_address_1",
"billing_address_2",
"billing_postcode",
"billing_company"
);
foreach($order as $field)
{
$ordered_fields[$field] = $fields["billing"][$field];
}
$fields["billing"] = $ordered_fields;
return $fields;
}
But this returns me no field I have created in the first step. I'ts clear that I'm missing a crusial part of the process, but I cannot figure out what exactly. Search on internet was no success (the methods provided there are adding fields before or after billing fields and I need to add it inside the group of billing fields).
Any help is appreciated! Thanks in advance!
Upvotes: 1
Views: 510
Reputation: 503
So I guess I found the answer. Actually in the first piece of code I have missed an important thing. That should look like that:
function custom_filter_checkout_fields($fields){
$fields['billing']['billing_title_field'] = array(
'some_field' => array(
'type' => 'text',
'required' => true,
'label' => __( 'Some field' )
)
);
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'custom_filter_checkout_fields');
and then the second piece of code to set the created field to the right place.
Upvotes: 0
Reputation: 441
add this plugin
http://phppoet.com/docs/checkout-fields/
and add your field you want
Upvotes: 1