我创建了一个插件,可以将订单报告发送到电子邮件地址。我需要将订单信息以CSV文件的形式获取,并作为附件与电子邮件一起发送。目前为止,我已经成功获取了订单信息,但是我在想如何在WooCommerce电子邮件中添加附件。
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);
我使用了这段代码来挂钩附件,所以当发送"order_information_report"时会添加CSV文件。但是这似乎不起作用。我尝试注释掉if语句,但仍然没有效果。有趣的是,我尝试在WooCommerce包含电子邮件文件夹中的电子邮件触发器上执行此操作。
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());
}
print_r似乎返回csv文件的路径。当我在浏览器中输入该路径时,可以下载它。对于为什么可能无法正常工作,有什么建议吗?编辑
我忘记提到我收到了电子邮件,但没有附件。
baseurl
而不是上传目录的目录路径。而在你的答案中,你使用了插件目录的目录路径。附件只能使用文件路径而不是 URL。 - helgatheviking