1
votes

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.

1

1 Answers

2
votes

For anyone searching in the future, this gist will print certain meta keys in any email template that features the woocommerce_email_order_meta hook.

_my_field_1 and _my_field_2 are the meta keys for the Order's custom data that you are trying to display. I am not familiar with the Custom Order Data plugin, but the meta key for the OP may be _officer_text. In my example, I will use the meta key _some_field.

/**
 * Add the field to order emails
 **/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');

function my_custom_checkout_field_order_meta_keys( $keys ) {
    $keys['Some Field'] = '_some_field;
    return $keys;
}

For versions 2.3 and newer of WooCommerce:

// WooCommerce 2.3+
function kia_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
    $fields['some_field'] = array(
                'label' => __( 'Some field' ),
                'value' => get_post_meta( $order->id, '_some_field', true );
            );
    return $fields;
}
add_filter('woocommerce_email_order_meta_fields', 'kia_email_order_meta_keys', 10, 3 )

;