Richard Jacko
Richard Jacko

Reputation: 11

Add an email attachment to WooCommerce notifications when order status is on-hold

In woocommerce I am using following code for adding PDF files as email attachment:

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

function attach_terms_conditions_pdf_to_email ( $attachments , $id, $object ) {
    $your_pdf_path1 = get_stylesheet_directory() . '/pdf/ano1.pdf';
    $your_pdf_path2 = get_stylesheet_directory() . '/pdf/ano2.pdf';
    $attachments[] = $your_pdf_path1;
    $attachments[] = $your_pdf_path2;
    return $attachments;
}

My problem is that attachment is sending always for all email to customers. I would like to send email attachment only in case that Order status is "on-hold".

How is possible to know status of my order and send email attachment only for this case?

Upvotes: 1

Views: 2253

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253784

Updated

You need to use the $id argument with 'customer_on_hold_order' as email ID in your function as a condition…

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

function attach_terms_conditions_pdf_to_email ( $attachments , $id, $object ) {

// Continue if it's customer_on_hold email notiication
if ( $id != 'customer_on_hold_order' ) return $attachments;

    $your_pdf_path1 = get_stylesheet_directory() . '/pdf/ano1.pdf';
    $your_pdf_path2 = get_stylesheet_directory() . '/pdf/ano2.pdf';
    $attachments[] = $your_pdf_path1;
    $attachments[] = $your_pdf_path2;
    return $attachments;
}

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

Tested and works

Upvotes: 1

Related Questions