问题
Found this code on another thread but can't make it work. PDF uploaded to wp-content/child-theme/.
Goal is to attach the pdf to the completed order emails that woocommerce will send out.
Not sure if customer_completed_order
is correct?
add_filter( 'woocommerce_email_attachments', 'attach_terms_conditions_pdf_to_email', 10, 3 );
function attach_terms_conditions_pdf_to_email ( $attachments , $email_id, $email_object ) {
// Avoiding errors and problems
if ( ! is_a( $order, 'WC_Order' ) || ! isset( $email_id ) ) {
return $attachments;
}
if( $email_id === 'customer_completed_order' ){
$your_pdf_path = get_stylesheet_directory() . '/Q-0319B.pdf';
$attachments[] = $your_pdf_path;
}
return $attachments;
}
回答1:
There are some errors in your code: The $email_object
function argument is the wrong variable name and should be instead $order
to match with your first if statement.
Now for the attachement path linked to the theme, you will use:
get_stylesheet_directory()
for a child themeget_template_directory()
for a parent theme (Website without a child theme)
The email Id customer_completed_order
is correct to target Customer "completed" email notification.
As you are not using the $order
variable argument in your code, ! is_a( $order, 'WC_Order' )
is not needed, so the working code will be:
add_filter( 'woocommerce_email_attachments', 'attach_pdf_file_to_customer_completed_email', 10, 3);
function attach_pdf_file_to_customer_completed_email( $attachments, $email_id, $order ) {
if( isset( $email_id ) && $email_id === 'customer_completed_order' ){
$attachments[] = get_stylesheet_directory() . '/Q-0319B.pdf'; // Child theme
}
return $attachments;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and work.
For a parent theme replace:
$attachments[] = get_stylesheet_directory() . '/Q-0319B.pdf'; // Child theme
by the following line:
$attachments[] = get_template_directory() . '/Q-0319B.pdf'; // Parent theme
来源:https://stackoverflow.com/questions/56288324/add-a-pdf-attachment-to-woocommerce-completed-order-email-notification