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.
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!