WooCommerce电子邮件附件

5

我创建了一个插件,可以将订单报告发送到电子邮件地址。我需要将订单信息以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文件的路径。当我在浏览器中输入该路径时,可以下载它。对于为什么可能无法正常工作,有什么建议吗?
编辑
我忘记提到我收到了电子邮件,但没有附件。

1
如果我必须猜测为什么这个失败而你的答案有效,那可能是因为你在这里使用了 baseurl 而不是上传目录的目录路径。而在你的答案中,你使用了插件目录的目录路径。附件只能使用文件路径而不是 URL。 - helgatheviking
是的,当我在提交答案后打印了第二个附件数组时,我想到可能是这种情况。 - Self Designs
1个回答

4

由于某些原因,当我将路径更改为插件目录而不是上传目录时,似乎可以正常工作。

function attach_file_woocommerce_email($attachments, $id, $object)
{           
    if($id == 'order_information_report')
    {
        $your_txt_path = woire_get_plugin_path() . '/test.txt'; 
        $attachments[] = $your_txt_path;
    }

    return $attachments;
}
add_filter('woocommerce_email_attachments', 'attach_file_woocommerce_email', 10, 3);

谢谢,我现在可以在邮件中发送附件了。如果我的邮件是欢迎邮件,那么如何获取用户ID呢? - Jitendra Popat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接