user9089431
user9089431

Reputation: 23

Add an attachment to admin email-notification once order is placed in Woocommerce

I am trying to send a PDF file to shop admin once the new order is placed. The issue with woocommerce_email_attachments hook is that email is sent to both customer and admin.

add_filter( 'woocommerce_email_attachments', 'attach_order_notice', 10, 3 );

function attach_order_notice ( $attachments, $id, $object ) {
    $pdf_path = get_template_directory() . '/notice.pdf';
    $attachments[] = $pdf_path;
    return $attachments;
}

At the moment both customer and admin receive new order emails (including attachments) and I expect both customer and admin receive new order emails but only send an attachment to admin.

Is this even possible?

Upvotes: 2

Views: 3657

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253784

The $id is the WC_Email ID that you can use to target a specific email notification like "New order" that is sent to admin when an Order have been successfully placed, this way:

add_filter( 'woocommerce_email_attachments', 'attach_order_notice', 10, 3 );
function attach_order_notice ( $attachments, $email_id, $order ) 
{
    // Only for "New Order" email notification (for admin)
    if( $email_id == 'new_order' ){
        $attachments[] = get_template_directory() . '/notice.pdf';
    }
    return $attachments;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and working

Upvotes: 4

Related Questions