Feature when clients cannot submit a proposal if they don’t have a condition

Hi,
I’m developing a feature so clients cannot submit a proposal if they don’t have a condition.
I’m using the filter hivepress/v1/templates/request_view_page/blocks to fetch block request_actions_primary, so users cannot see make a proposal before they meet the condition.
I’m doing like this:
hivepress()->template->fetch_block($blocks, 'request_actions_primary');
But I’m having a problem with some users and I can’t understand why, they don’t have the condition but they offering a proposal. How is that possible?
Do you know why this can happen? The if condition has some bug? This hook allows this kind of bugs? Browser problem?
Do you know another way to hide the make proposal if condition is not ok?
I tried to the inverse, always hide this block and if you meet the condition I show to client, but I don’t know how to do that.
Can you give me some guidelines? I’m a bit lost in here.
Regards

Hi,

There are 2 ways to do this, you can use our template hooks and fetch the block, removing it if some condition applies, another more hacky solution is adding custom CSS to a page via the wp_add_inline_style function to apply the display:none rule to the button.

Please post the full snippet for the request blocks hook, if the block is fetched correctly it should disappear.

Hi Ihor,
Thanks for the reply.
My function is like this:

add_filter(
	'hivepress/v1/templates/request_view_page/blocks',
	function($blocks, $template){

		$request = HivePress\Models\Request::query()->get_by_id( get_post() );

		if(!$request){
			return $blocks;
		}

        $vendor = HivePress\Models\Vendor::query()->filter(
            [
                'status' => [ 'auto-draft', 'draft', 'publish' ],
                'user'   => get_current_user_id(),
            ]
        )->get_first();

        if($vendor){

			if(get_current_user_id() === $request->get_user__id()){

				error_log('Vendor_id: '.$vendor->get_id().' is seeing his own request: '. $request->get_id());

			}else if(!canMakeProposal($vendor)){

				hivepress()->template->fetch_block($blocks, 'request_actions_primary');

				$message = 'User has not invoice configured, configure it please.';
		
				hivepress()->template->merge_blocks(
					$blocks,
					[
						'request_details_primary' => [
							'blocks' => [
								'configure_invoice' => [
									'type' => 'content',
									'content' => '<p style="color:#00d084;">'.$message.'</p><button id="configureInvoiceOffer" class="button button--secondary">Configure Invoice</button><br>
									<script type="text/javascript">
										document.getElementById("configureInvoiceOffer").onclick = function () {
											location.href = "'.redirectArtistInvoice().'";
										};
									</script>',
									'_order' => 0,
								]
							],
						],
				]
				);

            }
        }

		if($request->get_request_paid() === '1' && $request->get_request_status()){

			if(strcmp($request->get_request_status(),'publish')===0 ){
				hivepress()->template->merge_blocks(
					$blocks,
					[
						'request_details_primary' => [
							'blocks' => [
								'request_status' => [
									'type' => 'content',
									'content' => '<div class="hp-listing__status hp-status hp-status--'.$request->get_request_status().'">
													<span> Paid </span>
												</div>',
									'_order' => 0,
								],
							],
						],
				]
				);
			}
		}else if(strcmp($request->get_request_status(),'wc-cancelled')===0 ){

			hivepress()->template->merge_blocks(
				$blocks,
				[
					'request_details_primary' => [
						'blocks' => [
							'request_status' => [
								'type' => 'content',
								'content' => '<div class="hp-listing__status hp-status hp-status--'.$request->get_request_status().'">
												<span> Cancelled </span>
											</div>',
								'_order' => 0,
							],
						],
					],
			]
			);

		}

		$request_type = $request->get_request_creation_type();

		if($request_type){
			if($request_type === 'public'){
				$request_type = 'Public Request';
			}else{
				$request_type = 'Private Request';
			}

			hivepress()->template->merge_blocks(
				$blocks,
				[
					'request_details_primary' => [
						'blocks' => [
							'request_type' => [
								'type' => 'content',
								'content' => '<b>'.$request_type.'</b>',
								'_order' => 0,
							],
						],
					],
			]
			);
		}

        return $blocks;
	},
	1000,
	2
);

Is a bit complex, but the part that matters is the beggining where we fetch the block or not.
I add a new button so Artist can configure things before can submit an offer.
Well, as you said it should work, but I’m having some problems with some users.
So you think the problem should be on the condition, is showing the button make an offer for the vendor?
The function canMakeProposal has the validation and has a lot of logs, so it should show why vendor has conditions to see the button or not.
Regards

Sorry for the delay.

Please try to use the “merge_blocks” helper function this way:

$blocks = hivepress()->template->merge_blocks(...

Blocks will not be merged if not re-assigned, this helper returns a changed array of blocks. Also, at the beginning you can use $request=$template->get_context('request') without an extra query.

If it still doesn’t work please try to debug the snippet line by line to check where it fails, maybe the issue is with the condition or there’s another reason.

Hope this helps.