How to automatically attach files (PDF, or whatever you want) to WooCommerce emails

cart laptop

Have you ever thought how useful it would be to be able to automatically send a file to the emails that WooCommerce sends?

There are many situations in which it can be very interesting to send an attachment to your customers’ order emails, for example…

  • Catalogs of offers.
  • Rules for the use of a service.
  • Installation instructions.
  • Guides of all kinds.
  • Digital gifts.
  • Offer coupons.

You will see how easy it is.

How to automatically attach a different file to each product or variation

If you want that when a customer purchases a product or service from your online store, WooCommerce automatically sends a file to your customers, it is a very easy thing to do, and you do not have to configure anything unusual, or use codes or anything like that.

All you have to do is take advantage of WooCommerce’s downloadable file capability.

You might have thought that downloadable products were only intended for digital downloads, like eBooks, software, etc, but no, it is a WooCommerce functionality that you can use in any type of product or product variation.

Just check the “Downloadable” box on any type of product or variation.

Once you activate this box, the fields from which to include the file or files available for download will appear, which will reach the customer with the email of their order.

When the customer completes an order for that item or variation, they will automatically receive the corresponding email, and a section with the files available for download, with a direct link to download them.

Additionally, they will also find the download links in the corresponding section of their customer account in your online store.

Needless to say, it is important that you put a descriptive name to the download? The visible name can be something as descriptive as “Catalog of offers” or “Download here a discount coupon”, or “Welcome guide and rules for using the gym”, etc.

How to automatically attach the same file to all orders

It’s a different matter if you prefer to automatically send the same file regardless of the product, in all orders.

This can be especially useful when you always want to send a file that can be used for all orders, such as rules for using a coworking, a guide, coupons for future purchases, catalogs, etc.

The good part is that you don’t have to add a download to every product in your online store, the bad part is that WooCommerce doesn’t have any settings to do this easily, with a single click.

Fortunately it’s really easy to achieve, you just have to remove your fears of copying, pasting and customizing a few simple lines of code.

The first step is to open from your FTP client or the file manager of your hosting company the functions.php file of the theme you have active, or if you prefer to make your own plugin for customizations and, at the end of any of these files, add a code like the one in this example:

/* Add PDF to emails of proccesing and new order  */
add_filter( 'woocommerce_email_attachments', 'wphelp_email_files_woo', 10, 4 );
function wphelp_email_files_woo( $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'] . "/2020/10/catalogue-deals.pdf"; //Change the route and name for yours
    return $attachments;
}Code language: PHP (php)

This code will automatically attach a file to the order emails of your WooCommerce online store indicated in the email.

The first change you have to make is the file path, which in the example would be /2020/10/catalogue-deals.pdf.

The code assumes, through $upload_dir['basedir'], that the file is inside the /wp-content/uploads/ folder so, as in the example, you only have to put the path from there, that is, the year, month and name of the file.

If in your WordPress installation you do not save the uploads organized by year and month then you would only have to put the name of the file.

The next and final change is to decide which emails the file will be attached to, in the example the administrator’s new_order and the customer’s customer_processing_order, but you can change this and attach it to other emails. The complete list of identifiers you can use is this:

'cancelled_order' //cancelled order
'customer_processing_order' //processing order
'customer_invoice' //invoice
'customer_new_account' //customer new account
'customer_note' //note for the customer
'customer_on_hold_order' //on hold order
'customer_refunded_order' //refunds
'customer_reset_password' //reset password
'failed_order' //failed order
'new_order' //new orderCode language: JavaScript (javascript)

When you save your changes the next emails sent by WooCommerce to those you have decided to send a file to will receive it as an attachment, directly in their inbox.

Read this post in Spanish: Cómo adjuntar automáticamente archivos (PDF, o lo que quieras) a los emails de WooCommerce

How useful was this post?

Click on a smiley to rate it!

Average rating 5 / 5. Vote count: 2

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

1 thought on “How to automatically attach files (PDF, or whatever you want) to WooCommerce emails”

  1. Hi Hector!

    Can you please let me know what is the code for send the the downloable products as a attachments in the confirmation order email?

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top