Send Email Notification 48h After Booking End

I use RentalHive for a store that will offer renting of physical products.
Basically, what I want to do is send an email on the booking end date to both the host and the renter, to warn them that they have 48 hours to report any issues.

Here’s what I’ve tried so far:

add_action( 'hivepress/v1/models/booking/update_status',   'hpben_schedule_ended_action' );
add_action( 'hivepress/v1/models/booking/update_end_time', 'hpben_schedule_ended_action' );

function hpben_schedule_ended_action( $booking_id ) {
    $booking = \HivePress\Models\Booking::query()->get_by_id( $booking_id );

    if ( ! $booking ) {
        return;
    }

    hivepress()->scheduler->remove_action(
        'hivepress/v1/models/booking/ended',
        [ $booking->get_id() ]
    );

    if ( $booking->get_status() === 'confirmed' ) {
        hivepress()->scheduler->add_action(
            'hivepress/v1/models/booking/ended',
            [ $booking->get_id() ],
            $booking->get_end_time()
        );
    }
}

add_action( 'hivepress/v1/models/booking/ended', 'hpben_send_end_notifications' );

function hpben_send_end_notifications( $booking_id ) {
    $booking = \HivePress\Models\Booking::query()->get_by_id( $booking_id );

    if ( ! $booking ) {
        return;
    }

    $renter  = get_user_by( 'id', $booking->get_user__id() );
    $listing = \HivePress\Models\Listing::query()->get_by_id( $booking->get_listing__id() );
    $host    = $listing ? get_user_by( 'id', $listing->get_user__id() ) : null;

    $item_name   = $listing ? $listing->get_title() : 'предмета';
    $site_name   = get_bloginfo( 'name' );
    $admin_email = get_option( 'admin_email' );
    $headers     = [ 'Content-Type: text/plain; charset=UTF-8' ];

    if ( $host && $host->user_email ) {
        $subject = sprintf( '[%s] Резервацията приключи — имате 48 часа да репортвате проблем', $site_name );
        $message = sprintf(
            "Hello %s,\n\n" .
            "The reservation for \"%s\" has ended.\n\n" .
            "You have 48 hours to report a problem if the item is returned damaged." .
            "After 48 hours, the payment will be automatically released.\n\n" .
            "If you have a problem, please contact us immediately at %s.\n\n" .
            "Thank you,\n%s",
            $host->display_name,
            $item_name,
            $admin_email,
            $site_name
        );
        wp_mail( $host->user_email, $subject, $message, $headers );
    }

    if ( $renter && $renter->user_email ) {
        $subject = sprintf( '[%s] Резервацията приключи — моля върнете предмета', $site_name );
        $message = sprintf(
            "Hello %s,\n\n" .
            "The reservation for \"%s\" has ended.\n\n" .
            "Please return the item if you haven't already." .
            "Your deposit will be refunded within 48 hours if there are no issues.\n\n" .
            "Thank you,\n%s",
            $renter->display_name,
            $item_name,
            $site_name
        );
        wp_mail( $renter->user_email, $subject, $message, $headers );
    }
}

Doesn’t seem to work though… I’d appreciate any help!

Thanks for your message — our team will reply shortly.

In the meantime, you can also get instant help from our AI Assistant, familiar with all the docs and solutions we’ve shared over the years.

Hi,

The code seems to be correct overall, but there’s an issue here:

if ( $booking->get_status() === 'confirmed' ) {

Please try changing “confirmed” to “publish”, then I recommend testing the code again and first checking if the action is scheduled at all when you make and confirm a new booking (also if you edit the end date for an existing booking), it should appear in the Pending tab of Tools/Scheduled Actions.

Hope this helps

P.S. The support period for your Bookings license key seems to have expired – please consider renewing it if you still need assistance or developer guidance related to the booking functionality Renew Support | HivePress

This got it working! Thank you for everything, Ihor!
I’ll make sure to tell my client about extending the support period.

Cheers!

1 Like

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