Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Quick little snippet that will let you add a static PDF file (or in fact, any kind of file) to the WooCommerce Order Emails. Just add the following code to the ‘custom functions’ area of your functions.php file:
<?php
// http://bryceadams.com/add-pdf-woocommerce-order-email/
add_filter( 'woocommerce_email_attachments', 'attach_terms_conditions_pdf_to_email', 10, 3);
function attach_terms_conditions_pdf_to_email ( $attachments, $status , $order ) {
$allowed_statuses = array( 'new_order', 'customer_invoice', 'customer_processing_order', 'customer_completed_order' );
if( isset( $status ) && in_array ( $status, $allowed_statuses ) ) {
$your_pdf_path = get_template_directory() . '/terms.pdf';
$attachments[] = $pdf_path;
}
return $attachments;
}
@tabboy

This comment has been minimized.

Copy link

@tabboy tabboy commented Dec 10, 2015

$pdf_path does not exist. Rename it to $your_pdf_path

@renuprv

This comment has been minimized.

Copy link

@renuprv renuprv commented Dec 28, 2015

Hi, I have added the above code in my theme functions.php. But no attachment is added to the woocomerce email. My requirement is - I am using multisite(sub directory based). In checkout page, user will upload a file. I want to send that uploaded file to the admin on new order. I am using the below code -
function attach_terms_conditions_pdf_to_email ( $attachments , $id, $object)
{
$attachments[] = $file; //$file contains the full path along with filename uploaded by user like - http://mysite/cityname/wp- content/uploads/user/fmyfile.pdf
return $attachments;
}

Can you please suggest if the above code is correct?

Thanks.

@damiencarbery

This comment has been minimized.

Copy link

@damiencarbery damiencarbery commented Oct 12, 2016

@renuprv: I think that $file needs to be an absolute path on the web server, not a url.

@hauge75

This comment has been minimized.

Copy link

@hauge75 hauge75 commented Nov 9, 2016

You have to change line 9 to:
$your_path = dirname(FILE) . '/terms.pdf';

(If terms.pdf is located at the same level as functions.php)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment