'WooCommerce attach YITH pdf invoice to custom order status email
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?
Solution 1:[1]
add_filter('woocommerce_email_attachments', 'woocommerce_emails_attach_invoice', 10, 3);
function woocommerce_emails_attach_invoice($attachments, $email_id, $order) {
if (!is_a($order, 'WC_Order') || !isset($email_id)) {
return $attachments;
}
if ('invoice-due' == $order->get_status()) {
$uploads = wp_upload_dir();
$invoice_path = $uploads['basedir'].'/ywpi-pdf-invoice/Invoices/';
error_log($invoice_path . 'invoice'.$order->get_id().'.pdf');
$attachments[] = $invoice_path . 'invoice'.$order->get_id().'.pdf'; // Change the file location as per the requirement.
}
/*
* You can check with the desired email_id object as well ( eg:- 'new_order' )
if( 'custom_order' == $email_id ){
$attachments[] = get_template_directory() . '/invoice.pdf';
}
*
*/
return $attachments;
}
2 - Try using YITH methods
add_filter('woocommerce_email_attachments', 'yith_woocommerce_emails_attach_invoice', 10, 3);
function yith_woocommerce_emails_attach_invoice( $attachments, $status, $object ) {
if (!$object instanceof WC_Order) {
return $attachments;
}
if (class_exists('YITH_Invoice')) {
if ('invoice-due' == $object->get_status()) {
$invoice = new YITH_Invoice(yit_get_prop($object, 'id'));
if (!$invoice->exists) {
return $attachments;
}
$your_pdf_path = YITH_YWPI_DOCUMENT_SAVE_DIR . $invoice->save_path;
$attachments[] = $your_pdf_path;
}
}
return $attachments;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |