Woocommerce发送PDF附件,仅发送2个产品的电子邮件 [英] Woocommerce send PDF attachments in order email only for 2 products

查看:102
本文介绍了Woocommerce发送PDF附件,仅发送2个产品的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果客户购买了这2种产品中的任何一种,我想随身携带PDF订单确认电子邮件。



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

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

函数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;
}
返回$附件;
}

但是这会将PDF发送到所有订单电子邮件。
我想发送PDF只有当客户购买了这2种产品之一。



我想我需要添加条件与产品ID或东西。 p>

解决方案

您可以使用 $ order-> get_items()
所有您需要做的是循环此数组并检查相应的产品ID:

 函数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将触发电子邮件的产品
$ send_email = false;
$ order_items = $ order-> get_items();

foreach($ order_items as $ item){//循环订单项
if(in_array($ item ['product_id'],$ attachment_products)){//比较每个产品ID与列出的产品ids
$ send_email = true;
break; //发现一场比赛;退出循环
}
}

if($ send_email){
$ your_pdf_path = get_template_directory()。 /media/test1.pdf;
$ attachments [] = $ your_pdf_path;
}
}
返回$附件;
}


In woocommerce, I've got 2 products which has product instructions PDF.

If customer buys any of those 2 products, I want to send its PDF along with order confirmation email.

Right now I'm using this code to send PDF with order confirmation email -

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

But this sends PDF to all order email. I want to send PDF only when customer has bought one of those 2 products.

I think I need to add condition with product id or something.

解决方案

You may retrieve order items with $order->get_items() All you need to do is loop over this array and check for corresponding product ids :

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

这篇关于Woocommerce发送PDF附件,仅发送2个产品的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆