2015-02-07 39 views
3

在woocommerce,我有2个产品,其中有产品说明PDF。Woocommerce发送PDF附件,以便电子邮件只为2个产品

如果客户购买了这两种产品中的任何一种,我希望将其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或东西。

+0

你只是检查电子邮件(状态)的类型,而不是如果产品在订单内。 – Adrian 2016-03-11 07:42:24

回答

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; 
} 
相关问题