首页 > 解决方案 > 仅将 pdf 附加到 Woocommerce 中的特定电子邮件通知

问题描述

仅将 pdf 附加到woocomerce新订单电子邮件

我正在使用此代码,但 PDF 会附加到 woocommerce 中的每封电子邮件

 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 ) {

     $your_pdf_path = get_template_directory() . '/terms123.pdf';
    $attachments[] = $your_pdf_path;

    return $attachments;
 }

标签: phpwordpresswoocommercehook-woocommerceemail-notifications

解决方案


更新 2

Woocommerce中没有针对客户的“新订单”通知……根据启用的付款方式,目标通知可以是“客户暂停订单”或/和客户处理订单” (请参阅​​最后的部分)

以下将为客户保留订单电子邮件通知启用 PDF 附件:

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;
    }

    // Only for "Customer On Hold" email notification (for customer)
    if( $email_id === 'customer_on_hold_order' ){

        $your_pdf_path = get_template_directory() . '/terms123.pdf';
        $attachments[] = $your_pdf_path;
    }

    return $attachments;
}

代码在您的活动子主题(活动主题)的 function.php 文件中。测试和工作。


根据安装中启用的支付网关,您可以:

1)您可以改用“正在处理”电子邮件,替换此行:

if( $email_id === 'customer_on_hold_order' ){

这样:

if( $email_id === 'customer_processing_order' ){

2)您可以同时使用客户“保留”和“处理中”电子邮件,替换此行:

if( $email_id === 'customer_on_hold_order' ){

这样:

if( in_array( $email_id, array( 'customer_on_hold_order', 'customer_processing_order' ) ){

推荐阅读