Php Woocommerce仅为2种产品发送订购电子邮件的PDF附件
在woocommerce中,我有两个产品,其中有产品说明PDF 如果客户购买了这两种产品中的任何一种,我想将其PDF与订单确认电子邮件一起发送 现在我正在使用此代码发送PDF和订单确认电子邮件-Php Woocommerce仅为2种产品发送订购电子邮件的PDF附件,php,wordpress,email,pdf,woocommerce,Php,Wordpress,Email,Pdf,Woocommerce,在woocommerce中,我有两个产品,其中有产品说明PDF 如果客户购买了这两种产品中的任何一种,我想将其PDF与订单确认电子邮件一起发送 现在我正在使用此代码发送PDF和订单确认电子邮件- add_filter( 'woocommerce_email_attachments', 'attach_terms_conditions_pdf_to_email', 10, 3); function attach_terms_conditions_pdf_to_email ( $attachme
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或其他内容的条件。您可以使用
$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;
}
您只是检查了电子邮件的类型(状态),而没有检查产品是否在订单中。