Hide and make optional (not required) working together incorrectly

I am trying to hide the Booking Available To and From fields. I was able to accomplish that by using the snippet below. But, when the listing is added, it throws an error “Booking Available To: is required”. So, I added the second snippet to make it optional. I set the priority higher and lower, yet both didn’t work.

If I use both snippets together, the Booking Available To and From always show (not hidden despite the snippet) and they are correctly Optional. If I use just one of either, they work fine. I need to both hide and ensure the Required error does not show.

HIDE:

add_filter(
	'hivepress/v1/forms/listing_update',
	function( $form ) {
		unset( $form['fields']['booking_max_time'] );
		unset( $form['fields']['booking_min_time'] );

		return $form;
	},
	1000
);

And Don’t make required:

add_filter(
	'hivepress/v1/forms/listing_update',
	function( $form ) {
		$form['fields']['booking_max_time']['required'] = false;
		$form['fields']['booking_min_time']['required'] = false;

		return $form;
	},
	1000
);

Please try this PHP snippet

add_filter( 'hivepress/v1/models/listing/attributes', 'custom_booking_make_not_required_fields', 1000 );
add_filter( 'hivepress/v1/models/vendor/attributes', 'custom_booking_make_not_required_fields', 1000 );

function custom_booking_make_not_required_fields($attributes){
	if(isset($attributes['booking_max_time'])){
		$attributes['booking_max_time']['editable'] = false;
		$attributes['booking_max_time']['edit_field']['required'] = false;
	}
	
	if(isset($attributes['booking_min_time'])){
		$attributes['booking_min_time']['editable'] = false;
		$attributes['booking_min_time']['edit_field']['required'] = false;
	}
	
	return $attributes;
}
1 Like

That worked beautifully. Thank you!