I’m trying to get some data from an order in a woocommerce email template, but get_post_meta just returns false. This code works on the thankyou page. I have spent too much time on this. Any help would be appreciated. Thanks!
global $post;
echo "test!!!<br />";
$x = get_post_meta( $order->id, 'attendee_data', true );
$y = get_post_meta( $order->id, 'attendee_test', true );
echo $order->id . '<br />';
echo $x;
echo $y;
I’ve attached a picture of the sql as well as an email.
SQL: http://i.stack.imgur.com/zUFBa.png
Email: http://i.stack.imgur.com/Uqtih.png
The whole email template:
<?php do_action('woocommerce_email_header', $email_heading); ?>
<p><?php _e( "Your order has been received and is now being processed. Your order details are shown below for your reference:", 'woocommerce' ); ?></p>
<?php do_action( 'woocommerce_email_before_order_table', $order, $sent_to_admin, $plain_text ); ?>
<h2><?php echo __( 'Order:', 'woocommerce' ) . ' ' . $order->get_order_number(); ?></h2>
<table cellspacing="0" cellpadding="6" style="width: 100%; border: 1px solid #eee;" border="1" bordercolor="#eee">
<thead>
<tr>
<th scope="col" style="text-align:left; border: 1px solid #eee;"><?php _e( 'Product', 'woocommerce' ); ?></th>
<th scope="col" style="text-align:left; border: 1px solid #eee;"><?php _e( 'Quantity', 'woocommerce' ); ?></th>
<th scope="col" style="text-align:left; border: 1px solid #eee;"><?php _e( 'Price', 'woocommerce' ); ?></th>
</tr>
</thead>
<tbody>
<?php echo $order->email_order_items_table( $order->is_download_permitted(), true, ( $order->status=='processing' ) ? true : false ); ?>
</tbody>
<tfoot>
<?php
if ( $totals = $order->get_order_item_totals() ) {
$i = 0;
foreach ( $totals as $total ) {
$i++;
?><tr>
<th scope="row" colspan="2" style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['label']; ?></th>
<td style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['value']; ?></td>
</tr><?php
}
}
?>
</tfoot>
</table>
<?php
global $post;
echo "test!!!<br />";
$x = get_post_meta( $order->id, 'attendee_data', true );
$y = get_post_meta( $order->id, 'attendee_test', true );
echo $order->id . '<br />';
echo $x;
echo $y;
foreach ( $x as $k => $p ) {
echo $k ." ... ". $p;
} ?>
<?php // attendee_order_details($order->get_order_number()) ?>
<?php do_action( 'woocommerce_email_after_order_table', $order, $sent_to_admin, $plain_text ); ?>
<?php do_action( 'woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text ); ?>
<h2><?php _e( 'Customer details', 'woocommerce' ); ?></h2>
<?php if ($order->billing_email) : ?>
<p><strong><?php _e( 'Email:', 'woocommerce' ); ?></strong> <?php echo $order->billing_email; ?></p>
<?php endif; ?>
<?php if ($order->billing_phone) : ?>
<p><strong><?php _e( 'Tel:', 'woocommerce' ); ?></strong> <?php echo $order->billing_phone; ?></p>
<?php endif; ?>
<?php wc_get_template( 'emails/email-addresses.php', array( 'order' => $order ) ); ?>
<?php do_action( 'woocommerce_email_footer' ); ?>
I did some testing based on your info and tried to reproduce your problems.
First I created a function which inserts the post meta (attendee_test and attendee_data) when placing an order (using the
woocommerce_checkout_update_order_meta
hook like you do).I added this in my themes
functions.php
(Note that the values are based on your input (image from db) and are not dynamic, just for testing):Then I added the following code to the template
customer-processing-order.php
:As you can see I modified it a bit to make it more clear. Also changed the $attendee_data loop because I think that wasn’t completely right (needed an extra foreach). Of course this isn’t related to the real problem.
Making a testorder will show the following data in the email:
This result demonstrate that
get_post_meta
is working in the email template based on the code above. (I used WordPress 4.0.1 and WooCommerce 2.2.10).If the test above is working fine also in your case then I think that the values are inserted in your database after the email is sent (they are inserted right, but to late).
Another thing you might want to check is resending the confirmation email. You can do this from the WordPress admin. When you edit an order you have a dropdown ‘Actions’ on the right side. You can select ‘Processing order’ under ‘Resend order emails’. Then click the refresh icon next to the dropdown and you will receive the order confirmation email again. See screenshot below:
This time you know for sure the values are already in the database before you’re sending the email.
Hope this information is helping you to solve the problem.
Well, ‘attendee_data’ is a serialized object so the third argument passed on the function get_post_meta must be set to false (or omitted).