John Davidson

php - WooCommerce attach YITH pdf invoice to custom order status email

0 comments
Message:


I am after some help, please.


I am looking for a code snippet that will attach my Yith PDF invoices to an automated email for a custom order status I have already created. I have set up a custom order status (called ‘invoice due’) that triggers an email (called ‘overdue payment’), however, the pdf invoice does not attach. The snippet could be made to attach the pdf invoice to all status emails if that is easier. The PDF invoice currently attaches to the order completed email, however the custom status email uses a different template (woocommerce-order-status-manager/templates/emails/customer-order-status-email.php) which doesn’t automatically attach the Yith pdf file. Any ideas?



My Answer:
Here is a code snippet that you can use to attach the YITH PDF invoice to your custom order status email:

php
add_action( 'woocommerce_email_attachments', 'attach_yith_pdf_invoice_to_custom_email', 10, 3 );

function attach_yith_pdf_invoice_to_custom_email( $attachments, $email_id, $order ) {
// Check if the email is your custom order status email
if ( $email_id == 'overdue_payment' ) {
// Get the YITH PDF invoice URL
$invoice_url = YITH_YWPI()->get_pdf_invoice_url( $order->get_id() );

// Check if the invoice URL is valid
if ( $invoice_url ) {
// Get the PDF content
$pdf_content = file_get_contents( $invoice_url );

// Add the PDF as an attachment
$attachments[] = array(
'name' => 'invoice.pdf',
'content' => $pdf_content,
'mime_type' => 'application/pdf'
);
}
}

return $attachments;
}


You can add this code to your theme's functions.php file or a custom plugin. Make sure to replace 'overdue_payment' with the ID of your custom email and adjust the code as needed for your specific setup.

This code will check if the email being sent is your custom order status email and if so, it will attach the YITH PDF invoice to the email.

Rate this post

5 of 5 based on 7832 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us