I've created a plugin that sends an order report to an email address. I need to get the order information in a CSV file and send it as an attachment with the email. So far, I've managed to get the order information but I am struggling figuring out how to attach a file in a WooCommerce email.
function attach_file_woocommerce_email($attachments, $id, $object)
{
if($id == 'order_information_report')
{
$upload = wp_upload_dir();
$order_csv_information = $upload['baseurl'] . '/order_information.csv';
$attachments[] = $order_csv_information;
}
return $attachments;
}
add_filter('woocommerce_email_attachments', 'attach_file_woocommerce_email', 10, 3);
I have used this code to hook onto the attachments so when the "order_information_report" is being sent it adds the csv file. However this seems to not work. I tried commenting out the if statement but it still didn't work.
Interestingly I tried this on the email trigger inside the WooCommerce includes email folder.
public function trigger($order_id) // Email Trigger
{
if(!$this->get_recipient())
{
return;
}
$this->send($this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments());
print_r($this->get_attachments());
}
The print_r seemed to return the path for the csv file. I can download it when I type that path in the browser. Any suggestions on to why this may not be working correctly?
Edit
I forgot to mention I get the email but with no attachment
baseurl
and not the directory path to the upload directory. And in your answer you are using the directory path to the plugin directory. Attachments only work with file paths and not URLs.