How to display acf product field in woocommerce order email

Advanced custom fields plugin is a very useful wordpress plugin if you want to display custom fields in your wordpress website. Also the combination of wordpress, woocommerce and ACF plugin, can give us a lot of flexibility in order to display custom fields in our ecommerce project.

In this example we will create a ACF field, display the field in a product page and then we will display the acf field in a woocomerce order email.

First we will install ACF plugin and create a field group.

How to display acf product field in woocommerce order email

We create an ACF text field and assign it to a woocommerce product.

Then we need to display the text field in the woocommerce email order template. In this case we will add in functions.php file the proper action, in order to display the filed. We will use the woocommerce_order_item_meta_end hook.

Open functions.php file, go to the end of line and add the code:

add_action( 'woocommerce_order_item_meta_end', 'custom_product_info', 20, 4 );
function custom_product_info ( $item_id, $item, $order, $plain_text ) {
$id = $item->get_product_id();
$product_info = get_field('customer_email',$id);
if($product_info){
echo "<p>Customer Message: $product_info</p>";
}
}

This code gets the product id and if there is product_info field, the action will add the text filed into the woocommerce order mail.

Also please consider to create a child theme and add the previous code into the child’ s theme function.php file, in order to make any modification without loosing them after an update.

Leave a Comment

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

0 Shares
Tweet
Share
Pin
Share