在woocommerce电子邮件通知中get_post_meta

Dav*_*vid 8 wordpress woocommerce

我正在尝试从woocommerce电子邮件模板中的订单中获取一些数据,但get_post_meta只返回false.此代码适用于thankyou页面.我花了太多时间在这上面.任何帮助,将不胜感激.谢谢!

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;
Run Code Online (Sandbox Code Playgroud)

我附上了sql的图片和电子邮件.

SQL:http://i.stack.imgur.com/zUFBa.png

电子邮件:http://i.stack.imgur.com/Uqtih.png

整个电子邮件模板:

<?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' ); ?>
Run Code Online (Sandbox Code Playgroud)

vic*_*nte 2

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):

add_action('woocommerce_checkout_update_order_meta', 'add_meta_values', 2);

function add_meta_values($order_id){

    // array with attendee data
    $attendee_data = array(
        array(
            'edit' => 'false',
            'company' => 'get',
        )
    );  

    add_post_meta( $order_id, 'attendee_data', $attendee_data );
    add_post_meta( $order_id, 'attendee_test', 'test' );

}
Run Code Online (Sandbox Code Playgroud)

Then I added the following code to the template customer-processing-order.php:

<?php

$attendee_data = get_post_meta( $order->id, 'attendee_data', true );
$attendee_test = get_post_meta( $order->id, 'attendee_test', true );

echo 'Order ID: ' . $order->id . '<br />';
echo 'Attendee Test: ' . $attendee_test . '<br />';
echo 'Attendee Data:<br />';

foreach ( $attendee_data as $k => $data ) {

    foreach ($data as $key => $value){

        echo $key . ' .. ' . $value . '<br />';

    }

} ?>
Run Code Online (Sandbox Code Playgroud)

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.