Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andrewlimaza/e6a7f09125d2902c785b3af375ae0a93 to your computer and use it in GitHub Desktop.
Save andrewlimaza/e6a7f09125d2902c785b3af375ae0a93 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( dirname( __file__ ) . '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);
@DhiaSlm
Copy link

DhiaSlm commented Jul 14, 2020

Hi Andrew,
here is the error message:
An error of type E_ERROR was caused in line 21 of the file /customers/0/2/2/fitcoach24.com/httpd.www/wp-content/plugins/pmpro-customizations/pmpro-customizations.php. Error message: Uncaught Error: Class 'Dompdf\Dompdf' not found in /customers/0/2/2/fitcoach24.com/httpd.www/wp-content/plugins/pmpro-customizations/pmpro-customizations.php:21
Stack trace:
#0 /customers/0/2/2/fitcoach24.com/httpd.www/wp-includes/class-wp-hook.php(287): my_init('')
#1 /customers/0/2/2/fitcoach24.com/httpd.www/wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters(NULL, Array)
#2 /customers/0/2/2/fitcoach24.com/httpd.www/wp-includes/plugin.php(478): WP_Hook->do_action(Array)
#3 /customers/0/2/2/fitcoach24.com/httpd.www/wp-settings.php(540): do_action('init')
#4 /customers/0/2/2/fitcoach24.com/httpd.www/wp-config.php(123): require_once('/customers/0/2/...')
#5 /customers/0/2/2/fitcoach24.com/httpd.www/wp-load.php(37): require_once('/customers/0/2/...')
#6 /customers/0/2/2/fitcoach24.com/httpd.www/wp-admin/admin.php(34): require_once('/customers/0/2/...')
#7 /customers/0/2/2/fitcoach24.com/httpd.www/wp-admin/plugins.php(10): require_once('/customers/0/2/...')
#8 {main}
thrown

I tried to update the folder hierarchy according to your code, so now it looks like this:

+wp-content/plugins
| +pmpro-customizations
| | +Dompdf/Dompdf
| | | +php-svg-lib
| | | +php-font-lib
| | | - ..
| | | - ..
| | - pmpro-customizations.php

but still facing a technical issue

@andrewlimaza
Copy link
Author

@duduslm, I've moved over to building a plugin for this instead. You can grab it off Github if you'd like to test it. I think the path on line 20 should be adjusted a bit further to be:

include( dirname(__file__) . '/dompdf/autoload.inc.php' );

@andrewlimaza
Copy link
Author

I have updated the code and it should fix the issue now.

@dloignon
Copy link

Problem with the logo
pmpro-pdf-invoices {{logo_image}}
image not found or type unknown

@andrewlimaza
Copy link
Author

Hi @dloignon

Sorry to hear about this issue, please can you reach out to https://yoohooplugins.com/support and we'll assist you further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment