Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Reorder Display of Fields on WooCommerce Bookings Form
/**
* A function to reorder the default display of fields on the WooCommerce Bookings form
* Put this function in your theme's functions.php file
*/
function custom_order_booking_fields ( $fields ) {
$reorder = array();
$reorder[] = $fields['wc_bookings_field_duration']; // Duration
$reorder[] = $fields['wc_bookings_field_resource']; // Resource
$reorder[] = $fields['wc_bookings_field_persons']; // Persons
$reorder[] = $fields['wc_bookings_field_start_date']; // Calendar or Start Date
return $reorder;
}
add_filter( 'booking_form_fields', 'custom_order_booking_fields');
@miladjef
Copy link

miladjef commented Jan 18, 2015

Thank's for that

I have question about woocommerce booking …
I want to change datepicker in date-picker.js and date-picker.min.js form gregorian calendar to jalali calendar …. But I cant change that.( I changed it when I create problem)
You can help me?
(I use woocommerce booking 1.5.1)

@rhian103
Copy link

rhian103 commented Feb 25, 2015

I'm using the above code and successfully moved the start date field to the top, but my persons fields are not displaying at all?? If anyone can shed light on this that would be great.

My code is:

function custom_order_booking_fields ( $fields ) {

$reorder  = array();
$reorder[] = $fields['wc_bookings_field_start_date'];  // Calendar or Start Date
$reorder[] = $fields['wc_bookings_field_duration'];  // Duration
$reorder[] = $fields['wc_bookings_field_persons'];  // Persons
$reorder[] = $fields['wc_bookings_field_resource'];  // Resource



return $reorder;

}
add_filter( 'booking_form_fields', 'custom_order_booking_fields');

@ymazuz
Copy link

ymazuz commented Mar 5, 2015

Does your Bookable Product use Person Types? If so, the Person field(s) is/are not $fields['wc_bookings_field_persons']. For each person the field has a unique key in the array that corresponds to the actual post ID of the Person Type in the database, e.g. $fields['wc_booking_field_persons_1234']. So your function would have to check for array entries with 'wc_booking_field_persons_' in the key and copy them into the reordered array.

@kerovito
Copy link

kerovito commented Sep 16, 2018

Hi Ymazuz,

Can you please help with the $fields['wc_booking_field_persons_1234']
I have few persons like adult, children but when i add the ids only shown the first one. How can I add multiple person field by order?

Thanks

@shaneonabike
Copy link

shaneonabike commented Nov 5, 2018

//Thanks for the awesome example I totally needed that//
You could also just pop the calendar off the existing fields and place it at the start.

$fields = array('wc_bookings_field_start_date' => $fields['wc_bookings_field_start_date']) + $fields;

@mikexavier
Copy link

mikexavier commented Jan 26, 2019

Hi, thanks for this! I get the following error when using though... do you get the same?

Notice: Undefined index: wc_bookings_field_duration
Notice: Undefined index: wc_bookings_field_resource

@webstersgr
Copy link

webstersgr commented Jan 29, 2019

Hello!!!
I have a question that I've been searching the web for an answer, but I got no luck yet!
My question is if there is a way to wrap each field with some html ?

What I specifically need is to wrap the persons field with some html in order to achieve a dropdown field with the persons types as options, instead of number type inputs. I've already override the number.php in my theme's files, but I need to add some opening & closing divs before and after the foreach loop inside persons_field() function.
Is this possible ?

Any help would be highly appreciated!

Thanks in advance!!!

@Ayzoh1
Copy link

Ayzoh1 commented Dec 1, 2020

Hello,
added the following, but the persons field still doesn't show at all
function custom_order_booking_fields ( $fields ) {

$reorder  = array();
$reorder[] = $fields['wc_bookings_field_duration'];  // Duration
$reorder[] = $fields['wc_bookings_field_resource'];  // Resource
$reorder[] = $fields['wc_bookings_field_persons_2793'];  // Persons
$reorder[] = $fields['wc_bookings_field_start_date'];  // Calendar or Start Date

return $reorder;

}
add_filter( 'booking_form_fields', 'custom_order_booking_fields');

any help fixing this

@TomasHurtz
Copy link

TomasHurtz commented Jun 2, 2021

@webstersgr I believe you could do that in the template file ? I need to add + - buttons for numbers field - did you manage that? If so, kindly share :-)

@Ayzoh1 I also need to do this. Did you find a solution?

[_wc_booking_has_person_types] => Array
(
[0] => 1
)

But how can we check the array in $reorder[] = $fields['wc_bookings_field_persons']; // Persons

["_persons"]=> array(1) { [0]=> int(1) }

I know in jquery we could use

                            $("input[id^='wc_bookings_field_persons_']").each(function() {
			var id = parseInt(this.id.replace("wc_bookings_field_persons_", ""), 10);
			var qty = $("#wc_bookings_field_persons_" + id);

But I don't know how to add this into the function above.

@Bocapio
Copy link

Bocapio commented Jun 12, 2021

Thanks!

but if you site has custom person types the positions is quite different, so we did this:

$reorder  = array();
$reorder[] = $fields['wc_bookings_field_start_date'];  // Calendar or Start Date
$reorder[] = $fields['wc_bookings_field_duration'];  // Duration
$reorder[] = $fields['wc_bookings_field_resource'];  // Resource
foreach(array_keys($fields) as $field){
	if(strpos($field, 'wc_bookings_field_persons') === 0){    //handle with $fields['wc_bookings_field_persons_XXXX'] fields
		$reorder[] = $fields[$field];  // insert any person field
	}
}
	return $reorder;

@TomasHurtz
Copy link

TomasHurtz commented Jun 14, 2021

@Bocapio thank you, that is very helpful.

Do you know how we can add plus and minus buttons to the person type fields? I added buttons to the number.php file and also added a function but it not working right; here is my code: https://gist.github.com/TomasHurtz/2acb6a4142faeafd390a749df764eb02

@lp-ben
Copy link

lp-ben commented Jun 15, 2021

This has been incredibly helpful, thank you so much!

Very off topic but I'm curious if you'd have any insight on this - I have a person type to add a maximum of one additional person to the booking and since the number doesn't need to vary I'd like to just change it from a text/number field to a checkbox, do you have any ideas on how I might be able to accomplish that?

@TomasHurtz
Copy link

TomasHurtz commented Jun 15, 2021

@lp-ben you could try to change the template file 'number.php' and modify the html to show checkbox with something like this?

<input type="checkbox" value="1" id="custom" checked="checked" /><span>One selected</span>

I'm not sure how this would affect all products that use person types? Try and see if it works.

@lp-ben
Copy link

lp-ben commented Jun 15, 2021

@TomasHurtz I tried that and it looked correct but gave an error saying "The minimum persons per group is 1" even when it was checked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment