0

Attach pdf to ONLY woocomerce new order email

I am using this code but the PDF gets attched to every email in woocommerce

 add_filter( 'woocommerce_email_attachments', 'attach_terms_conditions_pdf_to_email', 10, 3);
 function attach_terms_conditions_pdf_to_email ( $attachments , $email_id, $email_object ) {

     $your_pdf_path = get_template_directory() . '/terms123.pdf';
    $attachments[] = $your_pdf_path;

    return $attachments;
 }
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Sonia
  • 1,909
  • 1
  • 11
  • 13

1 Answers1

2

Update 2

There is no "New order" notification for customers in Woocommerce… Depending on the payment methods enabled, the notifications to target can be "Customer On Hold Order" or/and Customer Processing Order" (see the section at the end)

The following will enable PDF attachment for Customer On Hold Order email notification:

add_filter( 'woocommerce_email_attachments', 'attach_terms_conditions_pdf_to_email', 10, 3 );
function attach_terms_conditions_pdf_to_email ( $attachments , $email_id, $email_object ) {
    // Avoiding errors and problems
    if ( ! is_a( $order, 'WC_Order' ) || ! isset( $email_id ) ) {
        return $attachments;
    }

    // Only for "Customer On Hold" email notification (for customer)
    if( $email_id === 'customer_on_hold_order' ){

        $your_pdf_path = get_template_directory() . '/terms123.pdf';
        $attachments[] = $your_pdf_path;
    }

    return $attachments;
}

Code goes in function.php file of your active child theme (active theme). Tested and works.


Depending on your enabled payment gateways in your installation, you can:

1) You can use "Processing" emails instead, replacing this line:

if( $email_id === 'customer_on_hold_order' ){

by this:

if( $email_id === 'customer_processing_order' ){

2) You can use Both Customer "On Hold" and "Processing" emails, replacing this line:

if( $email_id === 'customer_on_hold_order' ){

by this:

if( in_array( $email_id, array( 'customer_on_hold_order', 'customer_processing_order' ) ){
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • I want it for customer and not admin – Sonia Nov 15 '18 at 08:38
  • @Sonia I have updated my answer, with some explanations… – LoicTheAztec Nov 15 '18 at 09:03
  • For some reason it doesnt work for me. Where did you upload PDF document? @LoicTheAztec – Wed May 29 '19 at 18:20
  • @Wed on a child theme folder, you should use: `get_stylesheet_directory_uri()` instead (for the path). See this related recent answer: https://stackoverflow.com/questions/56288324/add-a-pdf-attachment-to-woocommerce-completed-order-email-notification/56289590#56289590 – LoicTheAztec May 29 '19 at 18:30
  • @LoicTheAztec I'm doing that. This is my code: add_filter( 'woocommerce_email_attachments', 'attach_terms_conditions_pdf_to_email', 10, 3 ); function attach_terms_conditions_pdf_to_email ( $attachments , $email_id, $email_object ) { // Avoiding errors and problems if ( ! is_a( $order, 'WC_Order' ) || ! isset( $email_id ) ) { return $attachments; } if( $email_id === 'customer_on_hold_order' ){ $attachments[] = get_stylesheet_directory() . '/terms.pdf'; } return $attachments; } – Wed May 29 '19 at 18:36
  • Question for LoicTheAztec. I used your code and had to change this line if ( $email_id === 'customer_on_hold_order' ){ to this one if( $email_id === array('customer_on_hold_order','customer_processing_order','customer_completed_order')){ Nothing happens, I am not getting any attachments to those mails. I could save the functions.php with this line `$email_id, array` because I was getting error about that comma ( , ) – PaskoB Jun 18 '19 at 17:50
  • If I use it in a plugin what dir should I use – Nordic Guy Dec 24 '19 at 21:05
  • @LoicTheAztec is this only for the Woocommerce Invoice and Packaging Slip Plugin or can it be used without it? – Nordic Guy Dec 24 '19 at 21:06
  • @NordicGuy This thread is not related to any pdf plugin. Happy Christmas! – LoicTheAztec Dec 25 '19 at 10:18