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

Marc Wag­ner

Janu­ary 23, 2023

3 min read|

Woo­Com­mer­ce allows you to add attach­ments to your order emails with just a few lines of code. With every order, for exam­p­le, the terms and con­di­ti­ons, pri­va­cy poli­cy or the can­cel­la­ti­on poli­cy are then trans­mit­ted direct­ly. Of cour­se, this also appli­es to other file for­mats. It does­n’t have to be a PDF docu­ment.

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

Just copy the fol­lo­wing code and pas­te it into the “functions.php” of your Word­Press the­me. You can find the file here: wp-content/themes/{name-des-child-themes}/functions.php.

Atten­ti­on: Befo­re you edit files, be sure to make a back­up of the file. Alter­na­tively, you can use Dupli­ca­tor to back up all your web­site data direct­ly. A tuto­ri­al for Dupli­ca­tor can be found here: Dupli­ca­tor Tuto­ri­al.

Now add the fol­lo­wing 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 atta­ched to every e‑mail.

How to attach multiple attachments to your WooCommerce emails #

It is also pos­si­ble to sim­ply attach mul­ti­ple files. For this we just need to adjust the PHP snip­pet from abo­ve a bit. Here is an exam­p­le:

/**
 * 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 direct­ly add three files.

Limit attachments to different email types #

Woo­Com­mer­ce sends various emails. The two snip­pets abo­ve are sent with every order. But you can rest­rict it even fur­ther by query­ing the $mail_id. Here is a list of all pos­si­ble 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 exam­p­le, if you only want to attach the terms and con­di­ti­ons to a new order, you can modi­fy the snip­pet from abo­ve as fol­lows:

/**
 * 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 attach­ments you have defi­ned will only be atta­ched to new orders. Of cour­se you can chan­ge this as you like by swap­ping “new_order” with a value from the list abo­ve.

88e86fcb816eff22bc917094df2862d8dd5c0e978b333e6dd5f36f808990c261 96

Arti­kel von:

Marc Wag­ner

Hi Marc here. I’m the foun­der of Forge12 Inter­ac­ti­ve and have been pas­sio­na­te about buil­ding web­sites, online stores, appli­ca­ti­ons and SaaS solu­ti­ons for busi­nesses for over 20 years. Befo­re foun­ding the com­pa­ny, I alre­a­dy work­ed in publicly lis­ted com­pa­nies and acqui­red all kinds of know­ledge. Now I want to pass this know­ledge on to my cus­to­mers.

Hast du eine Fra­ge? Hin­ter­lass bit­te einen Kom­men­tar