Add a button to 'send message' via the request page

Hello there!

We’d like to add a Send Message button on the request page - for example below the Make an Offer button - Need a few electrical repairs – ExpertHive

As I can see this topic was already discussed some time ago here and from what I understand it was on the road map.

Would like to find out if this feature was implemented because even though I have the lates version on the theme and plugins, I can’t find it.

And if it was not implemented, can you give an advice about the best approach to implement that in a child theme?
We have a developer who can try implementing that, but needs some help to get started.

This feature is really important for us, because vendors often have questions which need clarification before making an offer.

Hope you can help us :pray:

Thank you.

1 Like

Hi,

Please check the solution in this topic: Adding button "Send a message" on the Request page

​I hope this is helpful to you.

Hi Andrii!

Thank you for the reply.
I’ve tried the proposed solution, but I run into a few issues.

First of all a vendor could not see the new button I’m trying to add, and while debuging, I figured out that this happens due to the incorrect user ID on this line

$vendor = \HivePress\Models\Vendor::query()->filter(['user' => $user->get_id()])->get_first();

So, in order to fix that, i’ve replaced it with the $current_user->ID like so:

$current_user = wp_get_current_user();
   if ( ! ( $current_user instanceof WP_User ) ) {
          return;
   }
$vendor = \HivePress\Models\Vendor::query()->filter(['user' => $current_user->ID])->get_first();

This makes the button show up correctly. But When I try to send a message as a Vendor, it tells me “You can’t send messages to yourself.”

Could you help me figure out where the user IDs are being messed up in this situation?

The entire custom code I’m using is the following:

add_filter(
    'hivepress/v1/templates/request_view_page/blocks',
    function ( $blocks, $template ) {
      if(hivepress()->get_version( 'messages' )){
        $request = $template->get_context('request');
        
        if(!$request){
          return $blocks;
        }
        
        $user = $request->get_user();
        
        if(!$user){
          return $blocks;
        }

        $current_user = wp_get_current_user();
        if ( ! ( $current_user instanceof WP_User ) ) {
          return;
        }

        $vendor = \HivePress\Models\Vendor::query()->filter(['user' => $current_user->ID])->get_first();

        if(!$vendor){
          return $blocks;
        }
        
        $template->set_context('vendor', $vendor);
        
        return hivepress()->template->merge_blocks(
        $blocks,
        [
          'request_actions_primary' => [
            'blocks' => [
                'message_send_modal' => [
                  'type'        => 'modal',
                  'model'       => 'vendor',
                  'title'       => hivepress()->translator->get_string( 'send_message' ),
                  '_capability' => 'read',
                  '_order'      => 5,
  
                  'blocks'      => [
                    'message_send_form' => [
                      'type'       => 'message_send_form',
                      '_order'     => 10,
  
                      'attributes' => [
                        'class' => [ 'hp-form--narrow' ],
                      ],
                    ],
                  ],
                ],
  
                'message_send_link'  => [
                  'type'   => 'part',
                  'path'   => 'vendor/view/page/message-send-link',
                  '_order' => 10,
                ],
              ],
          ],
        ]
      );	
      }
      
      return $blocks;
    },
    1000,
    2
  );

Thanks

Please make sure that the current user is not the author of the request, you can debug this using error_log or var_dump, to make sure that the sender/recipient user IDs are different.

Hi @ihor.

Thank you for getting back to me.

Yes, I’ve already made sure the current user is not the author of the request.
I know how to debug, but the behaviour is weird.

With the same logged in user (a vendor user), I’m trying to send messages to requests created by different users. And the result is the same.

When I inspect the request Payload, no matter which Request page the vendor is visiting, when it tries to send the message, the recipient's value is the same - equal to the Vendor’s ID (which is wrong).

And I’m not sure how to change the recipient ID.

As mentioned earlier, the only changes I’ve made to the solution proposed here, was to fix the user ID by passing the ID of the currently logged in user. Before making that change, the Send message button would not even show up.

So basically I’ve change this line:

$vendor = \HivePress\Models\Vendor::query()->filter(['user' => $user->get_id()])->get_first();

into this one:

$current_user = wp_get_current_user();
   if ( ! ( $current_user instanceof WP_User ) ) {
          return;
   }
$vendor = \HivePress\Models\Vendor::query()->filter(['user' => $current_user->ID])->get_first();

I’m using the ExpertHive theme with a child theme and the following plugins:

Let me know if I have to provide more details. Or if you can give me a hint where to look in order to fix the recipient ID.

Thank you.

Sorry for the delay.

I assume the reason is here hivepress-messages/includes/blocks/class-message-send-form.php at master · hivepress/hivepress-messages · GitHub The message form in the Request template context always sets the current request user as the recipient and possibly overrides any code snippets.

You can try filtering the form itself to override this behavior.