如何将'WooCommerce Custom Order Data'插件创建的字段打印到电子邮件中

时间:2013-10-08 22:03:19

标签: wordpress woocommerce

我在结帐完成后安装了WooCommerce自定义订单数据插件以创建自定义字段。在自定义插件中,我使用woocomerce_thankyou-hook从订单中收集一些数据并将其保存为$ order-> custom-> officer_text。

我需要将自定义数据打印到admin-new-order-mail,但我不知道该怎么做。

echo $order->custom->officer_text
admin-new-order-mail.php中的

不起作用。

我可以在感谢页面上打印数据,所以我知道,它就在那里。但是在电子邮件模板中没有运气。

如何让它发挥作用?

编辑:

我忘了发布自定义插件的代码。

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;
  }
}

该字段紧跟在ul.order_details.bacs_details

之后打印

但如果我在thankyou.php上打印__($ order),则自定义数据不存在,。

插件中的

print_r($ order-&gt; custom)给了我:

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|foo@example.de||01xx 654 xxx xx|
       )

   )

我很高兴,我到目前为止,因为我不是一个真正的程序员,但我不知道,如何控制我的第一个小插件的输出。所以,如果有人可以向我展示一个“最佳实践”解决方案,那就太棒了。

1 个答案:

答案 0 :(得分:1)

对于将来搜索的任何人,此gist会在任何包含woocommerce_email_order_meta挂钩的电子邮件模板中打印某些元键。

_my_field_1_my_field_2是您尝试显示的订单自定义数据的元键。我不熟悉自定义订单数据插件,但OP 的元键可能_officer_text。在我的示例中,我将使用元键_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;
}

对于版本2.3及更新版的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 )