0
votes

I'm trying to hook into 'woocommerce_email_recipient_booking_confirmed' to update/add to the recipient field. The link below works perfectly for default WooCommerce emails (and their actions). The action works if I manually type in an email within the action but I need the email to come from the customer input (via a Custom Field on the product page).

I believe it is something to do with class-wc-email-booking-confirmed.php. When I compare it against class-wc-email-customer-completed-order.php

class-wc-email-booking-confirmed.php

public function trigger( $booking_id )

class-wc-email-customer-completed-order.php

public function trigger( $order_id, $order = false )

My code (which I haven't changed from the resource below):

add_filter( 'woocommerce_email_recipient_booking_confirmed', 'additional_customer_email_recipient', 9999, 2 ); // Completed Order

function additional_customer_email_recipient( $recipient, $order ) {

  if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;  // when I comment this out, an ajax error is thrown. This is why I believe the order object directly accessible yet.

    $additional_recipients = array(); // Initializing…

    foreach( $order->get_items() as $item_id => $item_data ){
        $email = wc_get_order_item_meta( $item_id, 'Email Address', true );

        if( ! in_array( $email, $additional_recipients ) && strpos( $recipient, $email ) === false )
            $additional_recipients[] = $email;
    }

    $additional_recipients = implode( ',', $additional_recipients);

    if( count($additional_recipients) > 0)
    {
        $recipient = ','.$additional_recipients;
    }
    return $recipient;
}

Send Woocommerce Order to email address listed on product page

1

1 Answers

0
votes

It was because it wasn't the order object, I just had to call get_order().

$order = $order->get_order();

Code:

add_filter( 'woocommerce_email_recipient_booking_confirmed', 'additional_customer_email_recipient', 10, 2 );

function additional_customer_email_recipient( $recipient, $order ) {

    $order = $order->get_order();

    $additional_recipients = array(); 

    foreach( $order->get_items() as $item_id => $item_data ){
        $email = wc_get_order_item_meta( $item_id, 'Email Address', true );

        if( ! in_array( $email, $additional_recipients ) && strpos( $recipient, $email ) === false )

            $additional_recipients[] = $email;
    }

    $additional_recipients = implode( ',', $additional_recipients);

    if( count($additional_recipients) > 0)
    {
        $recipient = $additional_recipients;
    }
    return $recipient;
}