首页 文章

WooCommerce电子邮件附件

提问于
浏览
4

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

Edit

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

1 回答

  • 2

    出于某种原因,当我更改插件目录而不是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);
    

相关问题