skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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');
    

  2. You can use the other method ;

    /**
     * Remove vendor from cancellation email notification headers.
     */
    function remove_vendor_from_cancellation_email_headers( $headers, $booking_id, $booking ) {
        // Remove the vendor email from the "To" headers.
        $to = isset( $headers['To'] ) ? $headers['To'] : '';
        if ( $to ) {
            $to = explode( ',', $to );
            $to = array_diff( $to, array( 'vendor' ) ); // Replace 'vendor' with the actual vendor email
            $headers['To'] = implode( ',', $to );
        }
    
        // Return the updated headers.
        return $headers;
    }
    add_filter( 'woocommerce_email_headers', 'remove_vendor_from_cancellation_email_headers', 10, 3 );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search