You have an WooCommerce store running on and you have lots of products on your store. But for some specific products, you may want some kind of email customization. For example, you want to attach a document (for example a PDF file) to the WooCommerce order confirmation email for these specific products once your customer purchases any of them.

Trying to find a plugin to accomplish this task? Well, many of you may not like to install and activate another new plugin because your store is already bloated with a bunch of plugins. Also you may get tired of finding any suitable plugin that exactly takes care of this task

In this tutorial, we are going to show you how can you send a PDF file or any attachment or documents via WooCommerce new order confirmation email to your customers without the help of a plugin.

How to attach documents to WooCommerce order confirmation email for a specific product category.

1. First, get the product category ID. You can easily get that by going to the product category edit page.

2. Upload the attachment to the media library (PDF or Image or any attachment supported by WordPress) and get the attachment ID from there. You can get the attachment ID by going to the attachment ID edit page.

3. Open your theme’s functions.php file in your favorite text editor. You may want to use an FTP application like Filezilla. Copy and paste the below code and replace the product category ID and attachment ID with your own.

/**
* Add attachment in WooCommerce order emails for a specific product category
*
*/
function cdxn_woocommerce_attachments_specific_product_category( $attachments, $email_id, $email_order ) {
    $args = array(
        'post_type'             => 'product',
        'post_status'           => 'publish',
        'posts_per_page'        => -1,
        'tax_query'             => array(
            array(
                'taxonomy'      => 'product_cat',
                'field' 		=> 'term_id', 
                'terms'         => 279, // This is the product category ID (not a product ID)
                'operator'      => 'IN'
            )
        )
    );
    $products = new WP_Query($args);
    $pr_arr = array();

    if( $products->have_posts() ) :
    while( $products->have_posts() ) : $products->the_post();
        $pr_arr[]= get_the_ID();
    endwhile; 
        else : 
    endif; 
    wp_reset_postdata();

    // Upload your attachment in the WordPress media library.
    // Get the attachment ID from there. 
    // It can be any image or PDF file or any attachment that is supported by WordPress.
    $attachment_id = 30314;

    // adding the attachment to the order confirmation email for the customer
    if( $email_id === 'customer_processing_order' ){
        $order = wc_get_order( $email_order );
        $items = $order->get_items();
        foreach ( $items as $item ) {
            $order_prod_id = $item->get_product_id();
            if (in_array($order_prod_id, $pr_arr)) {
                $attachments[] = get_attached_file( $attachment_id );
            }
        }
    }
   return $attachments;
}
// hook the function into WooCommerce email attachment filter
add_filter( 'woocommerce_email_attachments', 'cdxn_woocommerce_attachments_specific_product_category', 10, 3 );

Don’t forget to replace the product category ID and the attachment ID in the above code. Once you have replaced these two, save and upload the file to your server.

All done. From now on, whenever a customer purchases a product belonging to this specific category, he will receive the attachment in his order confirmation email.


How to attach documents to WooCommerce order confirmation email for a specific product.

In such a case where you want to deal with only one specific product and not with the entire product category, please continuing reading this post. Copy and paste the below code in your functions.php file. Replace the product ID and the attachment ID, save and upload the server.


/**
 * Add attachment in WooCommerce order email for a specific product
 *
 */
function cdxn_woocommerce_attachments_specific_product( $attachments, $email_id, $email_order ) {
	// Get the product ID
	$product_id = 7021; //This is the product ID (not product category ID)

	// Upload your attachment in the WordPress media library.
	// Get the attachment ID from there. 
	// It can be an image or PDF file or any attachment that is supported by WordPress.
	$attachment_id = 30314;

	// adding the attachment inside the order email that the customer is going to receive 
	if( $email_id === 'customer_processing_order' ){
		$order = wc_get_order( $email_order );
		$items = $order->get_items();
		foreach ( $items as $item ) {
			if ( $product_id === $item->get_product_id() ) {
				$attachments[] = get_attached_file( $attachment_id );		
			}
		}
	}
	return $attachments;
}
add_filter( 'woocommerce_email_attachments', 'cdxn_woocommerce_attachments_specific_product', 10, 3 );

So now, you know how to send a PDF file or any attachment to your customer in the new order confirmation email for a specific product or product category.

If you want to explorer more tutorials related to WooCommerce, please visit our WooCommerce blog.

Thank you.