I installed WooCommerce Custom Order Data PlugIn to create a custom field after checkout is completed. In a custom plugin I use the woocomerce_thankyou-hook to collect some data from the order and save it as a string to $order->custom->officer_text.
I need to print that custom data to the admin-new-order-mail, but I don't know how to do that.
echo $order->custom->officer_text
in admin-new-order-mail.php doesn't work.
I can print_r the data on the thank you-page, so I know, it's there. But no luck in the email-template.
How do I get it to work?
Edit:
I forgot to post the code of my custom plugin.
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
global $woocommerce;
// Action after order is submitted
add_action('woocommerce_thankyou', 'woocommerce_officer_data');
// function to populate a custom field in $order
function woocommerce_officer_data($order_id) {
$order = new WC_Order( $order_id );
// create custom field
WC_CustomOrderData::extend($order);
$order->custom->officer_text = '';
$officer = '';
// get date and format
$date = DateTime::createFromFormat('Y-m-d H:i:s', $order->order_date);
$orderdate = $date->format('d.m.Y');
$ordertime = $date->format('H:i:s');
$officer .= '##Officer-START##<br>Datum:' . $orderdate . '<br>Zeit:' . $ordertime . '<br>';
$officer .= $order_id . '|' . $order_id . '|' . $order->billing_first_name . '|' . $order->billing_last_name . '|';
$officer .= $order->billing_country . '|' . $order->billing_postcode . '|' . $order->billing_city . '|' ;
$officer .= $order->shipping_address_1 . '|' . $order->billing_email . '|' . $order->billing_phone . '|' ;
$order->custom->officer_text = $officer;
$order->custom->save();
echo $order->custom->officer_text;
}
}
The field is printed right after the ul.order_details.bacs_details
But if I print_r($order) on thankyou.php, the custom data is not there, .
print_r($order->custom) in the plugin gives me this:
WC_CustomOrderData Object
(
[order_id:WC_CustomOrderData:private] => 241
[fields:WC_CustomOrderData:private] => Array
(
[officer_text] => ##Officer-START##
Datum:09.10.2013
Zeit:12:00:38
241|241|Peter|Petersen|DE|11111|Xtown|Ystreet 53|[email protected]||01xx 654 xxx xx|
)
)
I'm happy, I got so far, because I'm not a real coder, but I have no idea, how to control the output of my first little plugin. So, if someone could show me a 'best practise' solution, it would be great.