How can I add attachments (T&Cs, privacy policy, etc.) to order emails in WooCommerce? (2023, Instruction)

WooCommerce allows you to add attachments to your order emails with just a few lines of code. With every order, for example, the terms and conditions, privacy policy or the cancellation policy are then transmitted directly. Of course, this also applies to other file formats. It doesn’t have to be a PDF document.

This PHP snippet adds a PDF attachment to your email orders #

Just copy the following code and paste it into the “functions.php” of your WordPress theme. You can find the file here: wp-content/themes/{name-des-child-themes}/functions.php.

Attention: Before you edit files, be sure to make a backup of the file. Alternatively, you can use Duplicator to back up all your website data directly. A tutorial for Duplicator can be found here: Duplicator Tutorial.

Now add the following code to the PHP file:

/**
 * Add an attachment to the WooCommerce mails.
 */
add_filter( 'woocommerce_email_attachments', 'attach_file_to_order_emails', 10, 3);
function attach_file_to_order_emails( $attachments , $mail_id, $order ) {

  if( ! is_a( $order, 'WC_Order' ) || ! isset( $mail_id) ) {
    return $attachments;
  }
  // use get_template_directory if you do not use a child theme.
  $path_to_file = get_stylesheet_directory() . '/agb.pdf';
  $attachments[] = $path_to_file ;
  return $attachments;
}

This means that the agb.pdf is now attached to every e-mail.

How to attach multiple attachments to your WooCommerce emails #

It is also possible to simply attach multiple files. For this we just need to adjust the PHP snippet from above a bit. Here is an example:

/**
 * Add multiple attachments to the WooCommerce mails.
 */
add_filter( 'woocommerce_email_attachments', 'attach_file_to_order_emails', 10, 3);
function attach_file_to_order_emails( $attachments , $mail_id, $order ) {

  if( ! is_a( $order, 'WC_Order' ) || ! isset( $mail_id) ) {
    return $attachments;
  }
  // use get_template_directory if you do not use a child theme.
  $path = get_stylesheet_directory();

  $attachments[] = $path.'agb.pdf';
  $attachments[] = $path.'datenschutz.pdf';
  $attachments[] = $path.'widerruf.pdf';
 

  return $attachments;
}

This will directly add three files.

Limit attachments to different email types #

WooCommerce sends various emails. The two snippets above are sent with every order. But you can restrict it even further by querying the $mail_id. Here is a list of all possible values:

  • 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

For example, if you only want to attach the terms and conditions to a new order, you can modify the snippet from above as follows:

/**
 * Add multiple attachments to the WooCommerce mails.
 */
add_filter( 'woocommerce_email_attachments', 'attach_file_to_order_emails', 10, 3);
function attach_file_to_order_emails( $attachments , $mail_id, $order ) {

  if( ! is_a( $order, 'WC_Order' ) || ! isset( $mail_id) || $mail_id !== '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.'datenschutz.pdf';
  $attachments[] = $path.'widerruf.pdf';
 

  return $attachments;
}

That’s about it. Now the attachments you have defined will only be attached to new orders. Of course you can change this as you like by swapping “new_order” with a value from the list above.

Hier klicken, um den Beitrag zu bewerten
[Gesamt: 0 Durchschnitt: 0]

Leave A Comment

Title