Search code examples
phpwordpresswoocommercehook-woocommerceemail-notifications

Attach pdf to only specific email notification in Woocommerce


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;
 }

Solution

  • 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' ) ){