Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce Email Attachment

Tags:

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

like image 220
Self Designs Avatar asked Jan 13 '17 10:01

Self Designs


1 Answers

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);
like image 132
Self Designs Avatar answered Sep 22 '22 09:09

Self Designs