• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Damien Carbery - Website Development

Getting You Online & Keeping You There

Getting You Online & Keeping You There Get Started
  • Home
  • Services
  • Maintenance Plans
  • Portfolio
  • Blog
  • About Me
  • Contact
You are here: Home / Website Development / Attach WooCommerce product docs to Contact Form 7 email

Attach WooCommerce product docs to Contact Form 7 email

Published: December 19, 2022

Create a brochure request form to send documents to a customer

Last year I wrote about attaching image files to the Order Completed email. I have also written about attaching a product image to a CF7 email, which was inspired by the product enquiry form post. Here I will use a bit of each of those to have a product brochure request form that emails product documents to a customer.

Use CMB2 to upload the documents

At the end of the Attach WooCommerce product image to Contact Form 7 email post I suggested a use case where a customer can request a brochure. I proposed a dropdown listing all the products. While the code below could be enhanced to facilitate that, here the brochure request form will be on the product page.

Here I use a CMB2 file_list field on the product page to upload the brochure files. CMB2 stores the attachment ID and url. As I need a filesystem path I will use the ID to retrieve that.

Extracting the Product ID

Again I use the Contact Form 7 Dynamic Text Extension plugin so that the form can have the post ID (which is also the product ID). Then I parse the submitted form to extract it and get access to the post meta where the uploaded files IDs are stored.

Contact Form 7 form and email

Again, I use the Contact Form 7 Dynamic Text Extension plugin. I use it to include a hidden field in the form. This field has the post ID (which is also the product ID). I also added a hidden field for the product name so that I can use that in the email subject and body.

Then in the email message body I put the product ID on a line with ‘Product ID:’. The filter function will search for this line to retrieve the product ID.
While the message body can, of course, contain other text – this line must be somewhere in there.

From attachment ID to file path

CMB2’s file_list field stores the attachment ID and file url. I need to get the file system path to attach the file to the email – get_attached_file() does just that and I can add it to the CF7 array of attachments.

Spam

It is possible that the form could be used to send the brochure files to someone that did not request them.

Requiring the recipient verify their email address before receiving the files would be a lot of work. I would likely create a new database table and either store the email address, product ID and timestamp and send an verification email with a link that would then trigger sending the email. Requests would time expire after a certain period e.g. 24 hours. That’s more code than I want to write this week!

The code

Related Posts

Attach files to WooCommerce order email

Upload files to an order to attach them to the WooCommerce Order Completed email.

Use CMB2 to create a carousel

Add photos to multiple groups and randomise group order in a carousel

Related Posts with CMB2

Create a manually selected list of related posts to encourage site visitors to read more.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Blog Categories

  • Being Green
  • Food
  • Grammar
  • Just Strange
  • Laziness
  • Personal
  • Portfolio
  • Race Reports
  • Shudder
  • Typo
  • Website Development
  • WooCommerce Tips
  • Facebook
  • Twitter

Copyright © 2013–2023 Damien Carbery · Using Genesis Framework · Privacy Policy