还有一个参数没有传递,其中包括WC\\u Email对象($object
实际上是电子邮件对象的指示器;对于“-”客户、产品或电子邮件)。
传递的最后一个参数应允许您访问WC_Email
确定收件人,从而确定用户(如果您将其附加到某些用户信息(如电子邮件或用户ID)中,则应提供必要的信息,以确定正确的文件)。
尽管如此,这个答案中的一些是推测性的,因为我无法测试您的特定用例。但是如果您查看了适当的过滤器(例如includes/emails/class-wc-emails.php),那么您应该能够找出可用的方法。
我首先要探讨一下:
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;
}