So fügst du Anhänge bei WooCommerce-Bestellungen hinzu

rd|jz / 30. August 2017 / , , , , / WooCommerce / 15 Kommentare

rundum.digital | blog - So fügst du Anhänge bei WooCommerce-Bestellungen hinzu

Für den Betrieb eines Online-Shops ist es wichtig, dass die Käufer über das Widerrufsrecht belehrt werden und diese Information müssen auch auf einem dauerhaften Datenträger wie z.B. Email-Anhang zur Verfügung gestellt werden.

Mit diesem Code in der functions.php, kannst du Anhänge per Email mit der Bestellbestätigung mitsenden – natürlich ohne Einsatz vom Plugin.

/* PDF ADF als Anhang senden */
function attach_agb_pdf_to_email ( $attachments_agb , $id, $object ) {
	$agb_pdf_path = get_template_directory() . '/woocommerce/attached-files/AGB.pdf';
	$attachments_agb[] = $agb_pdf_path;
	return $attachments_agb;
}
add_filter( 'woocommerce_email_attachments', 'attach_agb_pdf_to_email', 10, 3);
/* PDF Wiederrufsbelehrung als Anhang senden */
function attach_widerruf_pdf_to_email ( $attachments_widerruf , $id, $object ) {
	$widerruf_pdf_path = get_template_directory() . '/woocommerce/attached-files/Widerrufsbelehrung.pdf';
	$attachments_widerruf[] = $widerruf_pdf_path;
	return $attachments_widerruf;
}
add_filter( 'woocommerce_email_attachments', 'attach_widerruf_pdf_to_email', 10, 3);

Passe ggf. noch den Pfad und die Dateinamen an.

$widerruf_pdf_path = get_template_directory() . '/woocommerce/attached-files/Widerrufsbelehrung.pdf';

In meinem Bespiel liegen die Dateien AGB.pdf und Widerrufsbelehrung.pdf im wp-content/themes/theme-ordner/woocommerce/attached-files/

 

 

  1. von Björn am 22. März 2018 20:57 Uhr

    Wow! Not a single comment? I can’t believe it.
    Thanks, Jing, for your code, it really saved me some time. And it’s doing its job, I just tested.
    Again, thank you very much, highly appreciated!
    -Björn

    Antworten

    • von Jing Zhou am 23. März 2018 09:25 Uhr

      Thank you for your comment 🙂

      Antworten

  2. von Björn am 01. Mai 2018 01:33 Uhr

    Hey Jing,

    am using this snippet for a while now and it is working just flawless. 🙂
    Because I’m currently working on saving my changes from being overwritten by updates, I would like to attach the files from a source outside of my theme folder. I assume get_template_directory () is not the right command to do that, because in my tests attachments haven’t been send.
    I’m using ‚theme-customisation‘ as a plugin in which you can store your php, js and css snippets. It is located in the usual /wp-content/plugins directoy. Within this folder I created an email folder and put my attachments in there. The path I inserted is the following:
    ‚/plugins/theme-customisation-master/custom/emails/attached-files/Widerrufsrecht.pdf‘

    Do you have an idea on how to pick up these files and attach them to my mails again using your script?

    Thank you, best regards
    -Björn

    Antworten

  3. von Björn am 03. Mai 2018 17:22 Uhr

    Hey again,

    I found the solution in the meantime. For all your visitors who are facing similar problems:
    If you are also using a plugin from which you can add php/js/css code and you want to store your email attachments within this plugin to save them from being overwritten by your next theme update, replace the get_template function and change the path to your attachment like this:
    $agb_pdf_path = plugin_dir_path(__FILE__) . ‚emails/attached-files/AGB.pdf‘;
    My attachment is stored in wp-content/plugins/MyPlugin/emails/attached-files/

    Best regards,

    -Björn

    Antworten

    • von Jing Zhou am 03. Mai 2018 20:27 Uhr

      Hi Björn,

      I am very sorry, that I did not replied to your post.
      It’s very busy with daily business.

      You can also upload the file to WordPress Media and using the URL to that file, which is in the upload folder.

      Cheers,
      Jing

      Antworten

  4. von Björn am 04. Mai 2018 13:17 Uhr

    Hi Jing,

    no worries, I was glad to be able to figure it out. Upload folder is a good idea, too. Wondering why I didn’t come up with that. 🙂

    Have a great weekend,

    -Björn

    Antworten

  5. von Tom am 25. Juli 2018 12:02 Uhr

    Super! Vielen Dank für diesen Beitrag. Script funktioniert perfekt!

    Antworten

    • von Jing Zhou am 25. Juli 2018 12:31 Uhr

      Prima! 🙂

      Antworten

  6. von Sebastian am 19. März 2019 20:58 Uhr

    Super! Schade, dass solche wichtigen Dinge von Haus aus fehlen und super Leute der Allgemeinheit eine Lösung bieten. Kann man mit wenig Aufwand nur einem bestimmten Produkt einen speziellen Anhang mitsenden?

    Antworten

    • von rd|jz am 25. März 2019 11:45 Uhr

      Hallo Sebastian, auf Anhieb wüsste ich jetzt nicht wie man das umsetzen kann.

      Antworten

  7. von Alex am 27. Mai 2019 11:52 Uhr

    Mal eine blöde Frage, in welcher functions.php ist das?

    Ich habe da mehrere bei mir, je nach theme

    Antworten

    • von rd|jz am 27. Mai 2019 11:58 Uhr

      Hallo Alex,

      du fügst den Code nur in der functions.php vom aktiven Theme ein. Ist es ein Child-Theme, dann in der functions.php des Child-Themes.
      Es ist die functions.php im Hauptverzeichnis des (Child-)Themes.

      Gruß
      Jing

      Antworten

  8. von Simon Bresch am 27. November 2019 12:50 Uhr

    Falls ein Childtheme eingesetzt wird, muss der Pfad zu den PDF mit folgender Funktion geholt werden:

    Childtheme:
    $agb_pdf_path = get_stylesheet_directory_uri() . ‚/woocommerce/attached-files/AGB.pdf‘;

    Parenttheme (bzw. kein Childtheme):
    $agb_pdf_path = get_template_directory() . ‚/woocommerce/attached-files/AGB.pdf‘;

    Nutzt man „get_template_directory();“ werden die PDF Dateien im Parenttheme erwartet. Das wiederspricht dem Einsatz eines Childthemes. Ansonsten ein schickes Snippet. 🙂

    Antworten

    • von rd|jz am 27. November 2019 12:52 Uhr

      Vielen Dank! 🙂

      Antworten

  9. von Abud am 03. Mai 2020 18:47 Uhr

    Hallo,
    vielen Dank für die tolle Anleitung! Eine Frage hätte ich noch:
    Werden die PDF Anhänge bei jeder E-Mail an den Kunden mitgeschickt oder nur bei der ersten Bestellbestätigung? VG

    Antworten

Auf Björn's Kommentar antworten

Antworten abbrechen