How to access a vendor attribute from message hook

Hi, I’m implementing an extension that uses twilio to send an sms message whenever a vendor receives a normal message or booking request.

I’m using the hivepress/v1/models/message/create hook i.e add_action('hivepress/v1/models/message/create', [$this, 'handle_message']); for messages.
How can I access a specific vendor attribute (phone_number) within the handle_message function?

In addition, I’m using the hivepress/v1/models/booking/create hook i.e add_action('hivepress/v1/models/booking/create', [$this, 'handle_booking_request']); for booking requests. How would I access the same specific vendor attribute that I created (phone_number) within the handle_booking_request function?

Also, what is the proper way to find this information in the future?

Hi,

Please try using this sample code snippet for messages:

add_action(
	'hivepress/v1/models/message/create',
	function ( $message_id, $message ) {
		$recipient_id = $message->get_recipient__id();

		if ( ! $recipient_id ) {
			return;
		}

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

		if ( ! $vendor ) {
			return;
		}

		$phone_number = $vendor->get_phone_number();
	},
	1000,
	2
);

It fetches the vendor’s phone number if the message is sent to the vendor. You can access any other vendor attribute this way.

For the bookings, I recommend using hivepress/v1/models/booking/update_status hook instead, because the listing ID may not be available on the booking creation (it may be an empty booking draft). Using the status hook, you can check if the booking is pending/confirmed and fetch the related listing and then vendor, for example:

$listing=$booking->get_listing();
$vendor=$listing->get_vendor();
$phone_number=$vendor->get_phone_number();

Hope this helps

Thank you, this works well. I was also curious how I could get the URL of the message page to link in the text so that vendors can easily respond. Also, I’m implementing a custom attribute (type: checkboxes) so vendors can check which notifications they want to receive. How do I access each individual checkbox e.g. “Booking Requests” so that I can send the message if the vendor has the box clicked on and not send the message if the vendor has the box clicked off? Thank you for all the help