Save pdf from woocommerce email attachment

29 April, 2024 00:44:39
eliodata
Topics: 16
Messages: 49
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
29 April, 2024 01:03:23
E2Pdf
Support
Topics: 7
Messages: 3330
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!
29 April, 2024 02:17:44
eliodata
Topics: 16
Messages: 49
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.
29 April, 2024 08:59:58
E2Pdf
Support
Topics: 7
Messages: 3330
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!
30 April, 2024 06:01:14
eliodata
Topics: 16
Messages: 49
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?
30 April, 2024 06:11:32
E2Pdf
Support
Topics: 7
Messages: 3330
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!
1 May, 2024 05:11:32
eliodata
Topics: 16
Messages: 49
Ok sorry I thought you were talking about the e2pdf settings options. It is a "orders" template yes.
1 May, 2024 05:13:43
E2Pdf
Support
Topics: 7
Messages: 3330
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!
1 May, 2024 06:12:28
eliodata
Topics: 16
Messages: 49
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
1 May, 2024 06:26:22
E2Pdf
Support
Topics: 7
Messages: 3330
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!
1 May, 2024 07:58:41
eliodata
Topics: 16
Messages: 49
It works. Thank you again!
here is the full working snippet:
https://codefile.io/f/18yqusmwxT