p_d*_*ang 5 php pdf email wordpress 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;
}
Run Code Online (Sandbox Code Playgroud)
但这会将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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4728 次 |
| 最近记录: |