skip to Main Content

I want to attach the invoice as PDF to the WooCommerce Mail. Works great with static PDFs (like terms and conditions), but I need the option to work with variable PDF-Files (like invoices).

I use this filter:

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

and this function:

function attach_pdf_to_email ( $attachments, $status , $object ) {

$pdf_path = ABSPATH . "wp-content/uploads/terms.pdf";
$attachments[] = $pdf_path;

return $attachments;

}

Works perfectly. Now I want to change $pdf_path to this:

$pdf_path = ABSPATH . "wp-content/uploads/terms" . $order_id . ".pdf";

But I am not able to get the $order_id.

I tried:

global $order;

// First try
$order_id = $order->id;

// Second try
$order_id = $order->get_id();

// Third and fourth try (like above)
global $post;

The problem is, that the filter sends neither the order, nor the order id. Is there any way or idea, how I can achieve that?

4

Answers


  1. Try this code.

    add_filter( 'woocommerce_email_attachments', 'attach_pdf_to_email', 10, 3);
    
    function attach_agb_to_email ( $attachments, $status , $order ) {
    
        if ( empty( $order ) ) {
            return $attachments;
        }
    
        $order_id = $order->id;
        $pdf_path = ABSPATH . "wp-content/uploads/terms" . $order_id . ".pdf";
    
        $attachments[] = $pdf_path;
    
        return $attachments;
    
    }
    
    Login or Signup to reply.
  2. you can get order id from $object.
    try below code, i have tried your code and edited it to get order id

    function attach_pdf_to_email($attachments, $status, $object) {
        $order_id = method_exists($object, 'get_id') ? $object->get_id() : $object->id;
    
        $pdf_path = ABSPATH . "wp-content/uploads/terms" . $order_id . ".pdf";
    //  $pdf_path = ABSPATH . "wp-content/uploads/terms.pdf";
    
        $attachments[] = $pdf_path;
    
        return $attachments;
    }
    
    add_filter('woocommerce_email_attachments', 'attach_pdf_to_email', 10, 3);
    
    Login or Signup to reply.
  3. Try this code:

     add_filter( 'woocommerce_email_attachments', 'wh_attach_document_into_email', 10, 3 );
        function wh_attach_document_into_email ( $attachments, $email_id, $object ){
            $order_id = $object->order->get_order_number();
            //`enter code here`
            return $attachments;
        }
    

    Tested Woo > 3.8.0

    Login or Signup to reply.
  4. you have order object and from this object you can get order id i tried its working

    function af_ips_add_attachments($attachments, $email_id, $order, $email ){
        $af_e = $order->get_id();
        update_option('af_ips_emails',$af_e );
        $af_attachkment = WP_CONTENT_DIR .'uploadsaf-invoices/af- Invoice.pdf';
        $email_ids = array( 'customer_on_hold_order', 'customer_processing_order' );
        if ( in_array ( $email_id, $email_ids ) ) {
            $attachments[] = $af_attachkment;
        }
        return $attachments;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search