
Upload files to an order to attach them to the WooCommerce Order Completed email.
I previously wrote about conditionally attaching files to WooCommerce order email to the Completed order email.
A client asked how to attach custom photos of a product to the Completed order email. This meant that the list of files to attach to the email could not be a static list as it was for the other plugin. I copied and tweaked the meta box code form the Tracking Info to WooCommerce order post to allow for a list of files to be uploaded and associated with each order. CMB2 has the ‘file_list’ field type that allows the upload of multiple files. It is primarily intended for galleries but is perfect for this.
The files are attached via the ‘woocommerce_email_attachments‘ filter and checking that the email is the ‘customer_completed_order‘ one. The list of attachments is in the post meta and includes the attachment ID and url. The
url is not useful here as the path to the file is needed so it can be attached. The get_attached_file() API call retrieves this.
Upload before sending email
For the shop manager, the most important thing is to note is that the files must be uploaded before the order is marked Complete.
The code
The settings for the ‘file_list‘ field limits it to image uploads (‘query_args‘ parameter). This can be commented out or edited to allow for different file types.
Hey, is there a way to make this usable to add files to an order note and then send this to a customer? these files are individual per order and can change up to once a day. i would like to use woo to keep track of what was sent when. thanks
@Felix – Maybe the code could be changed to attach the files when you add a note to the order – on line 36, change ‘customer_completed_order’ to ‘customer_note’. To keep track, your note would have to say what was in the email, it would not be a record of the files sent.
Hi,
I tried implementing your code but I’m not sure what went wrong. I edited out the query_args parameter because I wanted to attach a PDF file. Upon testing with the last part back in the code, I just got an empty space with ‘ ‘ in the email body.
Can you help me? Cheers
Actually you can ignore my previous comment, I didn’t realize that by “the files must be uploaded before the order is marked Complete” you meant one has to do it in two steps, I was uploading the file and marking the order as complete at the same time before hitting the update button on the order ^^’
@Hugo – Yes, it has to be done in two steps. There’s an underlying issue that others have found in my tracking info code where the tracking info has to be added and then change order status. It’s on my todo list to investigate that. The solution to that may benefit the situation you see.
Hi,
I am trying to find a way so that the files being attached are not public, in my case the customer can order an assessment and i want to be able to attach that assessment to the order but it contains personal information.
Any ideas how to do this?
@Shane: Mmm, tough one.
I know that WooCommerce puts uploads under
wp-content/uploads/woocoommerce_uploads
which it protects with a .htaccess file. This means that the files cannot be accessed directly and have to go through the WooCommerce download script. This concept could be an option.I had a quick look at how it gets the uploaded file there – it uses the
'upload_dir'
filter (fromwp-includes/functions.php
) in woocommerce/includes/admin/class-wc-admin-post-types.php.So, you could probably do something similar – the post type would be a
'shop_order'
. There’s probably another filter to allow your new uploads directory to be visible to the CMB2 File Upload panel.If there isn’t too much data maybe you could store it with order meta and have an extra tab or section in the order details that the customer views.