0

Coming from this question adding-bcc-recipient-to-woocommerce-email-notification... I try similar but the ACF value is coming from the product.

I did the following:

add_filter( 'woocommerce_email_recipient_new_order', 'acf_recipient_new_email_notification', 15, 2 );
function acf_recipient_new_email_notification( $recipient, $order ) {
    if( class_exists( 'acf' ) ){
        $partner_email = get_field( 'email_recipient_new_order' );
    }
    foreach($order->get_items() as $item_id => $item ) {
        if( $partner_email ) {
            $recipient .= ', ' . $partner_email;
            break;
        }
    }
    return $recipient;
}

No result is given. What goes wrong here?

Demian
  • 536
  • 5
  • 26
  • 1
    `get_field` will use the ID of the current object by default, if you don't specify one. Not sure what that would be in this context, but pretty sure it will _not_ be a product. You will need to _get_ the product(s) via the order that was passed in to your callback function first, and then pass the product ID explicitly to `get_field`. And if you have not done something elsewhere to guarantee that each order will only ever contain _one_ product, you will probably also need to loop over the products, for this to start making sense. – CBroe Apr 04 '23 at 09:40
  • @CBroe thank you. You directed me to the right direction. – Demian Apr 04 '23 at 11:49

1 Answers1

0

Based on @CBroe i came with the following solution.

get_field needs to be defined in the loop based on the product_id of the item.

add_filter( 'woocommerce_email_recipient_new_order', 'acf_recipient_new_email_notification', 15, 2 );
function acf_recipient_new_email_notification( $recipient, $order ) {
    foreach($order->get_items() as $item_id => $item ) {
        $partner_email = get_field( 'email_recipient_new_order', $item->get_product_id() );
        if( $partner_email ) {
            $recipient .= ', ' . $partner_email;
            break;
        }
    }
    return $recipient;
}
Demian
  • 536
  • 5
  • 26