将 pdf 附件添加到 WooCommerce 已完成订单电子邮件通知

Jef*_*onk 2 php wordpress attachment email-notifications woocommerce

在另一个线程上找到此代码,但无法使其工作。PDF 上传到 wp-content/child-theme/。

目标是将 pdf 附加到 woocommerce 将发送的已完成订单电子邮件中。

不确定是否customer_completed_order正确?

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;
}
Run Code Online (Sandbox Code Playgroud)

Loi*_*tec 5

您的代码中存在一些错误:$email_object函数参数是错误的变量名称,应该$order与您的第一个 if 语句匹配。

现在,对于链接到主题的附件路径,您将使用:

  • get_stylesheet_directory()儿童主题
  • get_template_directory()对于父主题(没有子主题的网站)

电子邮件 IDcustomer_completed_order对于目标客户“已完成”电子邮件通知而言是正确的。

由于您没有$order在代码中使用变量参数,! is_a( $order, 'WC_Order' )因此不需要,因此工作代码将是:

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;
}
Run Code Online (Sandbox Code Playgroud)

代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并工作。


对于父主题替换:

$attachments[] = get_stylesheet_directory() . '/Q-0319B.pdf'; // Child theme
Run Code Online (Sandbox Code Playgroud)

通过以下行:

$attachments[] = get_template_directory() . '/Q-0319B.pdf'; // Parent theme
Run Code Online (Sandbox Code Playgroud)