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

时间:2019-05-24 08:05:25

标签: php wordpress woocommerce attachment email-notifications

在另一个线程上找到了此代码,但无法使其正常工作。 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;
}

1 个答案:

答案 0 :(得分:1)

您的代码中有一些错误:$email_object函数参数是错误的变量名,应改为$order以与您的第一个if语句匹配。

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

  • get_stylesheet_directory()(用于子主题)
  • get_template_directory()代表父主题(没有子主题的网站)

电子邮件ID customer_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;
}

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


对于父主题,请替换:

$attachments[] = get_stylesheet_directory() . '/Q-0319B.pdf'; // Child theme

通过以下行:

$attachments[] = get_template_directory() . '/Q-0319B.pdf'; // Parent theme
相关问题