Overriding output to render for reviews block when there are no reviews for the listing

Is there a filter I can use to customize what is rendered by the hivepress reviews block (or any other such block) such that if no reviews are found for the listing I can override what is rendered such that something like

No reviews have been submitted yet. Be the first! Submit a review

is rendered instead?

Any pointers as to the best way to accomplish this? Thank you.

Please try this PHP code snippet to start.

add_filter( 
	'hivepress/v1/templates/listing_view_page/blocks', 
	function($blocks, $template){
		$listing = $template->get_context('listing');
		
		if(!$listing){
			return $blocks;
		}
		
		$review_count = \HivePress\Models\Review::query()->filter(['listing' => $listing->get_id(), 'parent' => null, 'approved' => true])->get_count();
		
		if($review_count){
			return $blocks;
		}
		
		return hivepress()->template->merge_blocks(
			$blocks,
			[
				'page_content'            => [
					'blocks' => [
						'reviews_container' => [
							'type'   => 'section',
							'title'  => hivepress()->translator->get_string( 'reviews' ),
							'_order' => 100,

							'blocks' => [
								'custom_review_block' => [
									'type'      => 'content',
									'content' => 'Custom text',
									'_order'    => 10,
								],
							],
						],
					],
				],
			],
		);
	},
	1000,
	2
);

Thank you! I’ve added this code to the functions.php file in our child theme folder but it does not seem to have any effect. Do you have any advice for how to debug/troubleshoot this filter code? What should the html look like if the custom review block content is successfully merged? As it stands nothing is rendered at all for the Reviews (Related) block in our “Listing” template if there are no reviews.
Thanks for your assistance.

I’ve done some debugging and see that the custom_review_block is indeed being merged. It looks like this:

[reviews_container] => Array
(
    [type] => section
    [title] => Reviews
    [_order] => 100
    [blocks] => Array
        (
            [listing_reviews] => Array
                (
                    [type] => related_reviews
                    [_label] => Reviews (Related)
                    [_settings] => Array
                        (
                            [0] => columns
                        )

                    [_order] => 10
                )

            [custom_review_block] => Array
                (
                    [type] => content
                    [content] => Custom text
                    [_order] => 10
                )

        )

)

However, the custom content (‘Custom text’) does not appear anywhere in the rendered page. Do I need to add something to the Listing template to have this content get rendered on the page?

Using the PHP filter code snippet below I was able to achieve most of what I wanted to do:

add_filter( 
	'hivepress/v1/templates/listing_view_page/blocks', 
	function($blocks, $template){
		$listing = $template->get_context('listing');
		
		if(!$listing){
			return $blocks;
		}
		
		$review_count = \HivePress\Models\Review::query()->filter(['listing' => $listing->get_id(), 'parent' => null, 'approved' => true])->get_count();
		
		if($review_count){
			return $blocks;
		}
		
		return hivepress()->template->merge_blocks(
			$blocks,
			[
				'page_content'            => [
					'blocks' => [
						'reviews_container' => [
							'type'   => 'section',
							'title'  => hivepress()->translator->get_string( 'reviews' ),
							'_order' => 100,

							'blocks' => [
								'listing_reviews' => [
									'type'      => 'content',
									'content' => '<div><p>No reviews have been submitted yet.</p></div>',
									'_order'    => 10,
								],
							],
						],
					],
				],
			],
		);
	},
	1000,
	2
);

Thanks for the initial code snippet @yevhen . I was not able to incorporate the template code needed to include a link to “Write a Review” in the content. I think I need to somehow render the template from the hivepress-reviews extension (templates/listing/view/page/review-submit-link.php) in the content string value. Can you offer any clues as to how to go about doing that?

Thank you.

Thank you for waiting. Please try this PHP code snippet to move “Write a Review” button

add_filter( 
	'hivepress/v1/templates/listing_view_page/blocks', 
	function($blocks, $template){
		$listing = $template->get_context('listing');
		
		if(!$listing){
			return $blocks;
		}
		
		$review_count = \HivePress\Models\Review::query()->filter(['listing' => $listing->get_id(), 'parent' => null, 'approved' => true])->get_count();
		
		if($review_count){
			return $blocks;
		}
		
		return hivepress()->template->merge_blocks(
			$blocks,
			[
				'page_content'            => [
					'blocks' => [
						'reviews_container' => [
							'type'   => 'section',
							'title'  => hivepress()->translator->get_string( 'reviews' ),
							'_order' => 100,

							'blocks' => [
								'listing_reviews' => [
									'type'      => 'content',
									'content' => '<div><p>No reviews have been submitted yet.</p></div>',
									'_order'    => 10,
								],
								
								'custom_review_submit_link' => hivepress()->template->fetch_block($blocks, 'review_submit_link'),
							],
						],
					],
				],
			],
		);
	},
	1000,
	2
);

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