phpwordpresswoocommercehook-woocommercewoocommerce-bookings

How to remove vendor from Customer booking cancelled email


I am using WooCommerce Bookings. I've customized the customer-bookings-cancelled template and it works when a customer cancels a booking, but it is sending an email notification to both vendor and customer. I need to remove the vendor from this notification but cannot find any instruction on how to do this. Are there any classes or hooks I can use to strip the vendor from the notification.

// Hook into the 'woocommerce_email_recipient_customer_booking_cancelled' filter
add_filter( 'woocommerce_email_recipient_customer_booking_cancelled', 'custom_exclude_vendor_from_customer_email', 10, 2 );

function custom_exclude_vendor_from_customer_email( $recipient, $booking ) {
    // Get the customer email
    $customer_email = $booking->get_customer_email();

    // Get the vendor email(s) - Replace 'vendor' with the appropriate user role for your vendors
    $vendor_emails = array();
    $vendors = get_users( array( 'role' => 'vendor' ) );
    foreach ( $vendors as $vendor ) {
        $vendor_emails[] = $vendor->user_email;
    }

    // Remove vendor emails from the recipient list
    $recipient_emails = array_diff( array( $customer_email ), $vendor_emails );

    // Return the filtered recipient list (customer email only)
    return $recipient_emails;
}

Any direction here would be greatly appreciated.


Solution

  • On our model, we determined that the vendor profile would always be the second recipient and therefore a simple array option worked the best:

    function remove_vendor_from_cancellation_email( $recipients ) {
        $recipientsArr = explode(',', $recipients);
        $recipient = $recipientsArr[0];
        return $recipient;
    }
    add_filter( 'woocommerce_email_recipient_booking_cancelled', 'remove_vendor_from_cancellation_email');