Woocommerce仅在2个产品的订购电子邮件中发送PDF附件

时间:2015-02-07 12:29:28

标签: php wordpress email pdf woocommerce

在woocommerce中,我有2个产品有PDF产品说明。

如果客户购买了这两种产品中的任何一种,我想发送PDF以及订单确认电子邮件。

现在我使用此代码发送带有订单确认电子邮件的PDF -

add_filter( 'woocommerce_email_attachments', 'attach_terms_conditions_pdf_to_email', 10, 3); 

function attach_terms_conditions_pdf_to_email ( $attachments, $status , $order ) {

    $allowed_statuses = array( 'new_order', 'customer_invoice', 'customer_processing_order', 'customer_completed_order' );

    if( isset( $status ) && in_array ( $status, $allowed_statuses ) ) {
         $your_pdf_path = get_template_directory() . '/media/test1.pdf'; 
         $attachments[] = $your_pdf_path; 
    } 
return $attachments; 
}

但是这会将PDF发送到所有订单电子邮件。 我想只在客户购买这两种产品中的一种时发送PDF。

我想我需要添加带有产品ID的条件。

1 个答案:

答案 0 :(得分:0)

您可以使用$order->get_items()检索订单商品 您需要做的就是遍历此数组并检查相应的产品ID:

function attach_terms_conditions_pdf_to_email ( $attachments, $status , $order ) {

$allowed_statuses = array( 'new_order', 'customer_invoice', 'customer_processing_order', 'customer_completed_order' );

if( isset( $status ) && in_array ( $status, $allowed_statuses ) ) {

    $attachment_products = array(101, 102) // ids of products that will trigger email
    $send_email = false;
    $order_items = $order->get_items();

    foreach ($order_items as $item) { // loop through order items
        if(in_array($item['product_id'], $attachment_products)) { // compare each product id with listed products ids
            $send_email = true;
            break; // one match is found ; exit loop
        }
    }

    if($send_email) {
        $your_pdf_path = get_template_directory() . '/media/test1.pdf'; 
        $attachments[] = $your_pdf_path;
    }
} 
return $attachments; 
}