0

I'm working on a website (client-side in Angular) and basically use WordPress as a “headless CMS” to be able to use useful plugins like “Woocomerce”.

Problem: Based on customer input on the website, an individual PDF with a random name is generated and stored on the server (The file name is known client side). After the ordering process has been initiated, a customer-specific PDF must be sent.

I am aware that there is a hook (woocommerce_email_attachments) to add attachments, but I don't know how to assign the file name to the customer at this point.

add_filter( 'woocommerce_email_attachments', 'my_attachments', 10, 3);
function my_attachments( $attachments , $id, $object ) {
    
    $attachment_path = get_stylesheet_directory() . '/attachments/' . '<randomName>.pdf'; //How do I make the filename known here
    $attachments[] = $attachment_path;
    
    return $attachments;
}

Would be grateful for any solution :)

0

There's one more argument that you're not passing, and that includes the WC_Email object ($object is actually an indicator of the object the email is "for" - customer, product, or email).

That last argument passed should allow you to access the methods in WC_Email to figure out the recipient, and therefore the user (which should give you the necessary info to figure out the correct file if you're attaching it to some user info such as the email or the user ID).

With that said, some of this answer is speculative since I can't test your specific use case. But if you review the filter in place (such as in includes/emails/class-wc-emails.php), then you should be able to figure out what methods are available.

I'd start by exploring this:

add_filter( 'woocommerce_email_attachments', 'my_attachments', 10, 4 );
function my_attachments( $attachments , $id, $object, $wc_email ) {

    // Start by checking if this is an email to the customer.    
    if ( $wc_email->is_customer_email() ) {

        // Get the recipient
        $recipient = $wc_email->get_recipient();

        /*
         * Assuming the recipient is a single email address, you can use 
         * this to figure out the info to get the attachment (i.e. your 
         * "randomName" value, which presumably has to tie out to the 
         * user somehow). IF that's based on the customer's email address,
         * then you can check $recipient directly. If it's the user ID,
         * then use get_user_by( 'email', $recipient ) to get the user
         * object for the user.
         */

        $attachment_path = get_stylesheet_directory() . '/attachments/' . '<randomName>.pdf'; //How do I make the filename known here
        $attachments[] = $attachment_path;
    
    }
    return $attachments;
}
3
  • Hi, thanks for your reply. I will try that at the weekend. But I'm afraid, that I don't know how to handover the filename to the recipient before. So the assignment (clientside known) filename --> recipient is still missing.
    – schefa11
    Apr 29 '21 at 8:29
  • You noted the filename is known, but is the user who the file belongs to known? What is your method of identifying the user a specific file belongs to? When you generate the file, are you also in some way identifying the user (perhaps by saving a user meta field or something)? If you don't have something that identifies which user goes with which file, then you have absolutely no way of completing this process.
    – butlerblog
    Apr 29 '21 at 15:03
  • You are right, the filename is known when the user starts the purchase process by clicking a button that leads to "example.com/cart/?add-to-cart=XYZ". However, since this is just a link, I don't know how to append the metadata (filename). I think the user becomes created automatically at this time but I don't know how to assign the meta data. Hope this is understandable:)
    – schefa11
    May 1 '21 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.