WooCommerce Email Attachment

假装没事ソ 提交于 2021-02-04 15:37:06

问题


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. 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.

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. However this seems to not work. I tried commenting out the if statement but it still didn't work.

Interestingly I tried this on the email trigger inside the WooCommerce includes email folder.

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. 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


回答1:


For some reason when I change the path to the plugin directory instead of the uploads directory it seems to work fine.

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


来源:https://stackoverflow.com/questions/41632921/woocommerce-email-attachment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!