简体   繁体   English

WooCommerce电子邮件附件

[英]WooCommerce Email Attachment

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. 我需要在CSV文件中获取订单信息,并将其作为电子邮件的附件发送。 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. 到目前为止,我已经设法获取订单信息,但我正在努力弄清楚如何在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);

I have used this code to hook onto the attachments so when the "order_information_report" is being sent it adds the csv file. 我已经使用此代码挂钩附件,因此当发送“order_information_report”时,它会添加csv文件。 However this seems to not work. 然而,这似乎不起作用。 I tried commenting out the if statement but it still didn't work. 我试着评论if语句,但它仍然无法正常工作。

Interestingly I tried this on the email trigger inside the WooCommerce includes email folder. 有趣的是,我在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());
}

The print_r seemed to return the path for the csv file. print_r似乎返回了csv文件的路径。 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 我忘了提到我收到了电子邮件,但没有附件

For some reason when I change the path to the plugin directory instead of the uploads directory it seems to work fine. 出于某种原因,当我更改插件目录而不是uploads目录的路径时,它似乎工作正常。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM