Skip to content

Instantly share code, notes, and snippets.

@lmartins
Forked from bryceadams/gist:a8e01ad4e1901f849658
Last active February 23, 2018 19:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmartins/9234b5983b62e66fc87d to your computer and use it in GitHub Desktop.
Save lmartins/9234b5983b62e66fc87d to your computer and use it in GitHub Desktop.
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
Copy link

tabboy commented Dec 10, 2015

$pdf_path does not exist. Rename it to $your_pdf_path

@renuprv
Copy link

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
Copy link

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

@hauge75
Copy link

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