0
votes

In Woocommerce, I need to print a custom field on the Completed Order email.

The template is completely unique in my themes woocommerce/emails folder because the default template doesn't work for my needs.

As such I'm trying to display the following: Title, Quantity and two custom fields

This needs to be included in the customer-completed-order.php email template but I am not certain what syntax is necessary to print it.

I found something by Googling but it doesn't work:

<?php foreach ( $items as $item_id => $item ) :
$_product     = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
?>

<p>Class: <?php echo get_post_meta($_product->id,'name',true); ?></p>

<p>Date: <?php echo get_post_meta($_product->id,'date_text_field',true); ?></p>

<p>Time: <?php echo get_post_meta($_product->id,'time_text_field',true); ?></p>

<p>Number of Spaces: <?php echo get_post_meta($_product->id,'qty',true); ?></p>


<?php endforeach; ?>
1
Are the class/date/time the same for every order? Or are they specific to a particular order?helgatheviking

1 Answers

1
votes

Try adding to action hook,
do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order );
OR
do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order );

This could be used in following way:

add_action( 'woocommerce_order_item_name', 'action_custom_order_meta', 10, 3 );

OR

add_action( 'woocommerce_order_item_name', 'action_custon_order_meta', 10, 3 );

Following is the callback handler function.

function action_custom_order_meta($item_name, $item, $false){
    $product_id = $item['product_id'];
    $product_custom_date = get_post_meta($product_id, 'product_custom_date', true);
    $product_custom_time = get_post_meta($product_id, 'product_custom_time', true);
    $product_custom_meta = 'Date: ' . $product_custom_date . ' <br> Time: ' . $product_custom_time;
    echo $product_custom_meta;
}

Change the custom field names to appropriate. You might also need to wrap the output according to the required markup for your custom email.

Hope this helps.