Woocommerce send PDF attachments in order email only for 2 products Woocommerce send PDF attachments in order email only for 2 products wordpress wordpress

Woocommerce send PDF attachments in order email only for 2 products


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