Add a pdf attachment to WooCommerce completed order email notification

一曲冷凌霜 提交于 2019-12-08 02:20:31

问题


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 theme
  • get_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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!