Add attachments based on user role to WooCommerce new user registration email

Rate this post

I want to add two pdf files to the new user registration e-mail of WooCommerce.

Because I have two specific user roles customer and seller. I want to send all new sellers two pdf files from paths $path1 and $path3 and all new customers two pdf files from paths $path2 and $path3.

I tried this in my functions.php

function attach_to_email ($attachments, $userrole) { 

$root = ABSPATH;
$path1 = $root . '/media/AGB H.pdf';
$path2 = $root . '/media/AGB K.pdf';
$path3 = $root . '/media/W.pdf';

if ( $userrole === 'seller' ) {
   $attachments[] = $path1;
   $attachments[] = $path3;
} else {
   $attachments[] = $path2;
   $attachments[] = $path3;

return $attachments;


add_filter( 'woocommerce_email_attachments', 'attach_to_email', 10, 2 );

In the e-mail template for sellers I call:

do_action( 'woocommerce_email_attachments', null, 'seller' );

But in the function, I always enter the else part and not the if-part. In addition, all e-mails are attached with the else-files now, not only the registration e-mails. Any ideas?



To only assign the attachments to the registration e-mail, you can use:

  • The $email_id, where this is equal to customer_new_account

For the attachement path, linked to the theme, you can use:

  • get_stylesheet_directory() for a child theme
  • get_template_directory() for a parent theme
  • I would also recommend not to use spacing in the file name of the pdf file

You can then assign the correct attachments based on the user role

So you get:

function filter_woocommerce_email_attachments( $attachments, $email_id, $object, $email_object = null ) {
    // Use get_stylesheet_directory() for a child theme
    // Use get_template_directory() for a parent theme
    $path_1 = get_template_directory() . '/my-file-1.pdf';
    $path_2 = get_template_directory() . '/my-file-2.pdf';
    $path_3 = get_template_directory() . '/my-file-3.pdf';

    // Customer new account email
    if ( isset( $email_id ) && $email_id === 'customer_new_account' ) {         
        // Get user role(s)
        $roles = (array) $object->roles;
        // Seller
        if ( in_array( 'seller', $roles ) ) {
            $attachments[] = $path_1;
            $attachments[] = $path_3;           
        // Customer
        } elseif ( in_array( 'customer', $roles ) ) {
            $attachments[] = $path_2;
            $attachments[] = $path_3;
    return $attachments;
add_filter( 'woocommerce_email_attachments', 'filter_woocommerce_email_attachments', 10, 4 );

Code goes in functions.php file of your active theme. Tested in WooCommerce 5.0.0

Leave a Reply

Your email address will not be published.

Back to top button