Adding an attachment to an email after an order in WooCommerce

This snippet allows you to automatically attach a file to WooCommerce emails sent after an order is placed or completed. It is commonly used to send documents such as Terms and Conditions, return policies, user manuals, or informational PDFs to customers.

The attachment is added only to specific email types (for example, “Processing order” or “Completed order”), and the file is loaded directly from the WordPress uploads directory. The snippet also checks whether the file exists, preventing email errors caused by missing attachments.

This solution is ideal for stores that need to meet legal requirements or want to reliably deliver important documents to customers without manual handling.

PHP
// pridanie prílohy k e-mailu po objednávke
add_filter('woocommerce_email_attachments', 'pridat_prilohu_po_objednavke', 10, 3);

function pridat_prilohu_po_objednavke($attachments, $email_id, $order) {
    // Pridávať prílohu iba k určitým typom e-mailov
    if ($email_id === 'customer_processing_order' || $email_id === 'customer_completed_order') {
        // Získať cestu do adresára uploads
        $upload_dir = wp_upload_dir(); 
        $subor_cesta = $upload_dir['basedir'] . '/XXXXXXXXXXXXXX.pdf';

        // Skontrolovať, či súbor existuje
        if (file_exists($subor_cesta)) {
            $attachments[] = $subor_cesta; // Pridať cestu k súboru do príloh
        }
    }

    return $attachments;
}

Customer Emails $email_id

These email IDs are used for emails sent to customers. They are commonly used when adding attachments, modifying email content, or applying conditional logic based on the email type.

customer_new_account

– New customer account created

customer_processing_order

– Order received / processing
👉 Most common email sent after successful payment

customer_completed_order

– Order completed
👉 Ideal for manuals, terms & conditions, certificates, or final documents

customer_on_hold_order

– Order on hold (waiting for payment)

customer_cancelled_order

– Order cancelled

customer_refunded_order

– Order refunded

customer_invoice

– Customer invoice / payment request

customer_note

– Customer note added by the store admin

customer_reset_password

– Password reset email

Leave a Reply

Your email address will not be published. Required fields are marked *