Save pdf from woocommerce email attachment

April 29, 2024 00:44:39
eliodata
Topics: 19
Messages: 57
Hi!

I use e2pdf shortcode into woocommerce emails additionnal content box to generate pdf attachments.
It works well. I would like to store the pdf as well. I tried some shortcodes without result. Can you help please?
Here is my last try:
[e2pdf-save id="16" attachment="true" dir="wp-content/uploads/testdevispdf/]
The pdf is attached to the email, but not saved.

Regards
April 29, 2024 01:03:23
E2Pdf
Topics: 7
Messages: 3617
Support
Hello,

Please, recheck:

1. Be sure that all attributes are closed correctly:

[e2pdf-save id="16" attachment="true" dir="wp-content/uploads/testdevispdf/"]

* The shortcode in your message has an unclosed dir attribute: dir="wp-content/uploads/testdevispdf/"

2. Be sure that the folder is created and writable.

3. If it still doesn't work, try to use the absolute path instead, as:

[e2pdf-save id="16" attachment="true" dir="/home/...../wp-content/uploads/testdevispdf/"]

* You can get the absolute path by enabling debug mode at "E2Pdf" -> "Settings" and checking the "E2Pdf" -> "Debug" page.

We would really appreciate your feedback at WordPress.org!
April 29, 2024 02:17:44
eliodata
Topics: 19
Messages: 57
Thank you for the quick answer!
The attribute was unclosed... sorry for that.
Do you think it is possible to dynamically create the folder? I tried this:

add_action('woocommerce_order_status_changed', 'generate_pdf_on_status_change', 10, 4);

function generate_pdf_on_status_change($order_id, $old_status, $new_status, $order) {
$user_info = get_userdata($order->get_user_id());
$username = $user_info->user_login; // Récupère le nom d'utilisateur de WordPress

$uploads_dir = wp_upload_dir();
$pdf_dir = trailingslashit($uploads_dir['basedir']) . 'devispdf/' . $username . '/' . $order_id . '/';

// Assurez-vous que le répertoire existe
if (!file_exists($pdf_dir)) {
wp_mkdir_p($pdf_dir);
}

// Mettez ici le code pour générer le PDF
// Vous pouvez utiliser e2pdf avec quelque chose comme ça :
echo do_shortcode('[e2pdf-save id="1" dir="' . $pdf_dir . '" create_dir="true" create_index="true"]');
}

the path is well created and the pdf attached to the email. But it isn't saved into the created folder.
April 29, 2024 08:59:58
E2Pdf
Topics: 7
Messages: 3617
Support
Hello,

Here is the corrected code that should work:

https://codefile.io/f/dymD5DmvYU

Corrections:

It was added "dataset" and "apply" attributes. It's required in case called from PHP. We suppose your E2Pf has a connection to "WooCommerce" -> "Orders".

It has been removed The function that creates a folder, as when it is used "create_dir" - E2Pdf will check and create it if it does not exist.

We would really appreciate your feedback at WordPress.org!
April 30, 2024 06:01:14
eliodata
Topics: 19
Messages: 57
Thank you!
I don't use woocommerce > orders
Just paste the shortcode into the email additionnal content box.
Should I paste the shortcode from your code instead?
April 30, 2024 06:11:32
E2Pdf
Topics: 7
Messages: 3617
Support
Can you let us know please the connection of the E2Pdf Template? It requires setting the correct "dataset" attribute when the shortcode is called from PHP.

We would really appreciate your feedback at WordPress.org!
May 01, 2024 05:11:32
eliodata
Topics: 19
Messages: 57
Ok sorry I thought you were talking about the e2pdf settings options. It is a "orders" template yes.
May 01, 2024 05:13:43
E2Pdf
Topics: 7
Messages: 3617
Support
Then yes, the updated code should work. If still not, let us know please, and we will do additional checks.

We would really appreciate your feedback at WordPress.org!
May 01, 2024 06:12:28
eliodata
Topics: 19
Messages: 57
Still nothing sorry. Maybe I'm doing something wrong.
Here the working code to create the correct path: https://codefile.io/f/18yqusmwxT
But it doesn't save the pdf into the file.
and
[e2pdf-save id="16" attachment="true" dir="wp-content/uploads/testdevispdf/"]
Works great to save the pdf.

thank you again
May 01, 2024 06:26:22
E2Pdf
Topics: 7
Messages: 3617
Support
You are missing the "dataset" and "apply" attributes in your PHP snippet, as it's mandatory when shortcode is called from PHP:

echo do_shortcode('[e2pdf-save id="1" dataset="' . $order_id . '" dir="' . $pdf_dir . '" create_dir="true" create_index="true" apply="true"]');

We would really appreciate your feedback at WordPress.org!
May 01, 2024 07:58:41
eliodata
Topics: 19
Messages: 57
It works. Thank you again!
here is the full working snippet:
https://codefile.io/f/18yqusmwxT
Get News and Updates
Our monthly newsletter keeps you in the loop, so you'll always be up to date with the latest news and updates about E2Pdf.
* You can update your email subscription preferences here.
All trademarks, service marks, trade names, product names, and logos appearing on this website are the property of their respective owners. The use of trademarks such as Formidable Forms®, Forminator Forms®, Gravity Forms®, JetFormBuilder®, WordPress®, WooCommerce®, WPForms® and others is solely for identification purposes (nominative use) and does not imply any affiliation, sponsorship, or endorsement by the respective trademark holders.
© 2017 - 2026, https://e2pdf.com

We use cookies to collect and analyse information on site performance and usage, to provide social media features and to enhance and customise content and advertisements. By clicking "Accept Selected", you consent to use of selected cookies according to our Cookie Policy.