Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save greathmaster/ea7cab507af67b9bc0af41ea7d3e84e2 to your computer and use it in GitHub Desktop.
Save greathmaster/ea7cab507af67b9bc0af41ea7d3e84e2 to your computer and use it in GitHub Desktop.
Adds a PDF invoice attachment to checkout emails.
<?php
/*
Add a PDF attachement of the invoice to the checkout email. This code gist must be configured...
1) Download the DOMPDF library: https://github.com/dompdf/dompdf
1.1) Save to wp-content/your-plugin/
2) Download the PHP SVG library: https://github.com/PhenX/php-svg-lib
2.1) Save to dompdf/lib/
3) Download the PHP FONT library: https://github.com/PhenX/php-font-lib
3.1) Save to dompdf/lib/
Upon checkout, will create a folder (...wp-content/invoices/[user_name]/ and add a PDF invoice. The content is the exact copy of the email.
*/
use Dompdf\Dompdf;
function my_init()
{
include 'dompdf/autoload.inc.php';
global $dompdf;
$dompdf = new Dompdf();
}
add_action('init', 'my_init');
function my_pmpro_email_attachments($attachments, $email)
{
global $dompdf;
//make sure it's a checkout email (but not the admin one)
if(strpos($email->template, "checkout_") !== false && strpos($email->template, "admin") === false)
{
//make sure attachments is an array
if(is_array($attachments))
$attachments = array();
$dompdf->loadHtml($email->body);
$dompdf->render();
$output = $dompdf->output();
$user_login = $email->data['user_login'];
$upload_dir = wp_upload_dir();
$user_dirname = $upload_dir['basedir'].'/invoices/'.$user_login;
if (!file_exists( $user_dirname ))
{
wp_mkdir_p( $user_dirname );
}
$date = date("Y-m-d");
$path = $user_dirname."/invoice-".$date.".pdf";
file_put_contents($path, $output);
//add our attachment
$attachments[] = $path;
}
return $attachments;
}
add_filter('pmpro_email_attachments', 'my_pmpro_email_attachments', 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment