WooCommerce – Beliebige Anhänge an E-Mails anfügen
In WooCommerce kann es manchmal wichtig werden, an bestimmte E-Mails eigene Anhänge anzufügen (z.B. AGB, Widerruf etc.). Als Pfad für die Dateien wird im Beispiel auf das Themes-Verzeichnis genutzt (via get_stylesheet_directory()).
/**
* Add attachments to the WooCommerce mails.
*/
add_filter( 'woocommerce_email_attachments', 'attach_file_to_order_emails', 10, 3);
function attach_file_to_order_emails( $attachments , $mailid, $order ) {
if( ! is_a( $order, 'WC_Order' ) || ! isset( $mailid) ) {
return $attachments;
}
// use get_template_directory if you do not use a child theme.
$path = get_stylesheet_directory();
$attachments[] = $path.'agb.pdf';
$attachments[] = $path.'widerruf.pdf';
return $attachments;
}
Die Option zum Anhängen von Dateien nur für bestimmte E-Mail Typen ist natürlich auch möglich. Anbei eine Liste von Typen auf die geprüft werden kann:
- new_order
- customer_on_hold_order
- customer_processing_order
- customer_refund_order
- customer_refunded_order
- customer_partially_refunded_order
- cancelled_order
- failed_order
- customer_reset_password
- customer_invoice
- customer_new_account
- customer_note
/**
* Add attachments to the WooCommerce mails.
*/
add_filter( 'woocommerce_email_attachments', 'attach_file_to_order_emails', 10, 3);
function attach_file_to_order_emails( $attachments , $mailid, $order ) {
if( ! is_a( $order, 'WC_Order' ) || ! isset( $mailid) || $mailid !== 'new_order' ) {
return $attachments;
}
// use get_template_directory if you do not use a child theme.
$path = get_stylesheet_directory();
$attachments[] = $path.'agb.pdf';
$attachments[] = $path.'widerruf.pdf';
return $attachments;
}