One of my clients has a complex online store that uses the WordPress WooCommerce plugin for their store front. In order to satisfy their requests, I had to utilize multiple custom fields to store data to specific products. The products were primarily online tutorial videos that needed to have custom links and unique password for each one of them. Adding new custom fields to the WooCommerce products page is a pretty straight forward process, but showing those custom fields values on the emails that are sent to the customers after they purchase the products, is where the challenge comes to play.
Although there are a few different ways to achieve this, such as using the Advanced Custom Fields plugin to add the custom fields to the products page, or using the Checkout Field Editor Extension for WooCommerce, I decided to take a more of a manual process for many different reasons – the client already had lots of plugins installed on her site and I wanted to make things easier and more customized for her.
Creating The Custom Fields
On the WooCommerce product page add the custom fields you wish to have. In this case, I’m adding “vid_link” and “vid_password” custom fields to store links and passwords respectively.
Displaying The Custom Fields Values to The Order Emails
Find “email-order-items.php” and place the following code wherever you want your custom fields to show on the email. This file pulls the information about the products that are being purchased in an order.
echo '<p><strong>Video Link:</strong><br />';
echo get_post_meta($_product->id, 'vid_link', true) .'</p>';
echo '<p><strong>Video Password:</strong><br />';
echo get_post_meta($_product->id, 'vid_password', true) .'</p>';
Be sure to change “vid_link” and “vid_password” to match whatever custom fields you’ve created on wp-admin. Note that I added some HTML to style how the custom fields will display on the email. You should change that according to your needs.
I hope this helps you on your WooCommerce projects.