Booking Feature Hidden if Attribute Set to "No"

I feel bad I’m asking so many questions, but… I’m really hoping this is the last one!

Is there a way to have the booking block be hidden IF a certain listing attribute is set to “No”?

You could try to make booking functionality category-specific by choosing categories in HivePress/Settings/Bookings. So booking functionality (also booking block) will work or will be visible only in listings that belong to chosen categories in this setting. If it is not the best solution for you then I can provide a code snippet which will hide the booking block depending on listing attribute value

Hi Yevhen,
Using the categories to limit the booking functionality won’t work in my case as I’m looking to have both bookable and non-bookable listings in a single category (for example, campsites that you can book versus some that are free/drop-in, but both are labeled as “campsites”).

If it’s not too much work, would it possible to get a code snippet for hiding the booking block depending on a listing attribute value? Thanks a million!

Please try this PHP snippet. If you use Checkbox attribute which has the name ‘Hide booking block’ for example then if this attribute is chosen in the listing booking block will be hidden

add_filter(
	'hivepress/v1/templates/listing_view_page/blocks',
	function ($blocks, $template){
		
		$listing = $template->get_context('listing');

		if($listing && $listing->get_hide_listing()){
			$blocks = hivepress()->helper->merge_trees(
					[ 'blocks' => $blocks ],
					[
						'blocks' => [
							'booking_make_form' => [
								'type' => 'content',
							],
						],
				]
				)['blocks'];
		}
		
		return $blocks;
	},
	1000,
	2
);

Thanks! Is there anything I need to modify in the snippet to reflect the name of my own attribute, like the part that says ‘content’? I don’t know how to read PHP to know what to modify (if I need to).

Yes, please change this part $listing->get_hide_listing() with $listing->get_your_attribute_slug()
So please change your_attribute_slug with your attribute slug

OK thanks!

I’ve tried to implement this but unfortunately, it doesn’t seem to work.

Maybe my attribute slug is wrong. I didn’t see anything listed on the WP backend so I found it by publishing the attribute and then inspecting the page code on the frontend. I believe my slug is business_owner but when I change that in the code, it doesn’t change anything whether I have it checked or not.

Here’s the bit that I thought was telling me the attribute slug:

hp-listing__attribute hp-listing__attribute--business-owner

Is there anything else that needs to change somewhere?

Please try this PHP snippet instead. If the attribute slug is correct the booking form should be hidden if the business owner field is empty.

add_filter(
	'hivepress/v1/templates/listing_view_page/blocks',
	function ($blocks, $template){
		
		$listing = $template->get_context('listing');

		if($listing && !$listing->get_business_owner()){
			$blocks = hivepress()->helper->merge_trees(
					[ 'blocks' => $blocks ],
					[
						'blocks' => [
							'booking_make_form' => [
								'type' => 'content',
							],
						],
				]
				)['blocks'];
		}
		
		return $blocks;
	},
	1000,
	2
);

Perfect that worked!

Just noticed though that the price field still shows up on the page because it’s mandatory to set a price (even if you don’t own the space and you set it to $0, it still displays $0/hr). Is there a way to also have that price field disappear alongside the booking form to make it 100% clear that the space can’t be booked?

Thanks!

If you mean to hide price field with the booking form then please try this PHP snippet

add_filter(
	'hivepress/v1/templates/listing_view_page/blocks',
	function ($blocks, $template){
		
		$listing = $template->get_context('listing');

		if($listing && !$listing->get_business_owner()){
			$blocks = hivepress()->helper->merge_trees(
					[ 'blocks' => $blocks ],
					[
						'blocks' => [
							'listing_attributes_primary' => [
								'attributes' => [
									'class' => [ 'custom-hide-booking-price-field' ],
								],
							],
							'booking_make_form' => [
								'type' => 'content',
							],
						],
				]
				)['blocks'];
		}
		
		return $blocks;
	},
	1000,
	2
);

Also, please add this CSS snippet to Appearance/Customize/Additional CSS

.custom-hide-booking-price-field .hp-listing__attribute--price{
	display: none;
}

I’m so so sorry, but this snippet + CSS code didn’t work. The price field is still appearing on the page (but the booking form stays hidden like it’s supposed to).

Please try this PHP snippet instead

add_filter(
	'hivepress/v1/templates/listing_view_page/blocks',
	function ($blocks, $template){
		
		$listing = $template->get_context('listing');

		if($listing && !$listing->get_business_owner()){
			$blocks = hivepress()->helper->merge_trees(
					[ 'blocks' => $blocks ],
					[
						'blocks' => [
							'page_sidebar' => [
								'attributes' => [
									'class' => [ 'custom-hide-booking-price-field' ],
								],
							],
							
							'booking_make_form' => [
								'type' => 'content',
							],
						],
				]
				)['blocks'];
		}
		
		return $blocks;
	},
	1000,
	2
);

Also, please add this CSS snippet to Appearance/Customize/Additional CSS

.custom-hide-booking-price-field .hp-listing__attribute--price{
	display: none;
}

Yes! That worked, thanks so much!

Although the pricing still shows up in the search (card) view of the listing. Is there a way to have the pricing field be hidden everywhere (on all pages and wherever this listing is displayed)? If it’s too complicated, no worries - just thought I’d ask in case it’s an easy tweak :slight_smile:

Please try to add this PHP snippet to hide the price attribute on the listing block by the same condition as it hides on the listing page

add_filter(
	'hivepress/v1/templates/listing_view_block/blocks',
	function ($blocks, $template){
		
		$listing = $template->get_context('listing');

		if($listing && !$listing->get_business_owner()){
			$blocks = hivepress()->helper->merge_trees(
					[ 'blocks' => $blocks ],
					[
						'blocks' => [
							'listing_attributes_primary' => [
								'attributes' => [
									'class' => [ 'custom-hide-booking-price-field' ],
								],
							],
						],
				]
				)['blocks'];
		}
		
		return $blocks;
	},
	1000,
	2
);

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.