How to Attach PDF Invoice to Email WooCommerce

WooCommerce Attach PDF Invoice to EmailAre you looking for a way to attach PDF invoices to the emails you send out to customers? WooCommerce continues to power many online stores in the world. This is because it is packed with many features that allow you to sell anything online.

One of the most outstanding features of WooCommerce is that it allows you to send out order emails to your customers. You may want to attach PDF invoices to the emails.

However, WooCommerce does not have a built-in solution to do this. There are 2 ways of doing this. You can use either a plugin or custom code.

It is important to note that plugins may bloat your site. The safest way to make changes is using custom code.

But we recommend creating a child theme so that your changes are not lost during an update.

WooCommerce Attach PDF Invoice to Email

In this brief tutorial, we will show you how to attach a PDF to emails you send out to customers. However, you need to have some coding skills to use this solution.

Let us see how you can achieve this.

Steps to Attach PDF Invoice to Email in WooCommerce

Since we will be attaching a file, we need to upload it somewhere on the site. We will upload the PDF file via the Media > Add New section of the WordPress dashboard.

Alternatively, you can place it in the file in your child theme folder. This means that you have to work with the get_stylesheet_directory().

For illustration purposes, we have uploaded a file called example.pdf and has been placed under /wp-content/uploads/2021/05/example.pdf

Here are the steps you need to follow:

  1. Log into your WordPress site and access the Dashboard as the admin user.
  2. From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file where we will add the function that will upload a PDF file to WooCommerce order emails.
  3. Add the following code to the php file:
[php] /**

* @snippet       Attach PDF file in WooCommerce Emails

*/

add_filter( ‘woocommerce_email_attachments’, ‘njengah_attach_pdf_to_emails’, 10, 4 );

function njengah_attach_pdf_to_emails( $attachments, $email_id, $order, $email ) {

$email_ids = array( ‘new_order’, ‘customer_processing_order’ );

if ( in_array ( $email_id, $email_ids ) ) {

$upload_dir = wp_upload_dir();

$attachments[] = $upload_dir[‘basedir’] . "/2021/05/example.pdf";

}

return $attachments;

}
[/php]

  1. Remember to add the correct directory and save the file.

Conclusion

In this brief tutorial, we have shared a simple solution to attach PDF files in the order emails that you send out to customers.

If you want advanced functionality, you can use a plugin to achieve this. We recommend creating a child theme, as this will ensure your changes are not lost during an update.

Similar Articles