我创建了一个将订单报告发送到电子邮件地址的插件。我需要在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文件的路径。当我在浏览器中键入该路径时,可以下载它。关于为什么这可能无法正常工作的任何建议?

编辑

我忘了提到我收到了电子邮件,但没有附件

最佳答案

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

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);

关于php - WooCommerce电子邮件附件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41632921/

10-11 19:21
查看更多