Attach a PDF to Order Completed Emails
Attaches a file, such as your terms and conditions, a user guide or a returns form, to the WooCommerce "Order completed" email.
Opens WPIDE on
Code
add_filter( 'woocommerce_email_attachments', function ( $attachments, $email_id ) {
$attachment_id = 0; // Media Library attachment ID.
$file_path = ''; // Or an absolute path on the server.
$email_ids = array( 'customer_completed_order' ); // Emails that get the file.
if ( ! in_array( $email_id, $email_ids, true ) ) {
return $attachments;
}
$path = $attachment_id ? get_attached_file( $attachment_id ) : $file_path;
if ( $path && is_readable( $path ) ) {
$attachments[] = $path;
}
return $attachments;
}, 10, 2 );
Notes
Upload the PDF to the Media Library and set $attachment_id, or set $file_path to an absolute server path.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.