如何在WooCommerce电子邮件中包含自定义域?

时间:2019-01-03 作者:Bradley Joe

我使用以下方法在签出页面上添加了一个自定义字段:

add_action( \'woocommerce_checkout_fields\', 
\'woo_add_conditional_checkout_fields\' );
function woo_add_conditional_checkout_fields( $fields ) {
foreach( WC()->cart->get_cart() as $cart_item ){
    $product_id = $cart_item[\'product_id\'];
            $fields[\'billing\'][\'billing_field_newfield\'] = array(
                \'label\'     => __(\'New Field\', \'woocommerce\'),
                \'placeholder\'   => _x(\'\', \'placeholder\', \'woocommerce\'),
                \'required\'  => true,
                \'class\'     => array(\'form-row-wide\'),
                \'clear\'     => false
            );
        }
return $fields;
}
现在,我想在“新订单”woocommerce电子邮件中包括用户在该字段中输入的任何内容,但是当我进入woocommerce>设置>电子邮件>新订单并将{billing\\u field\\u newfield}放在主题中时,我只在电子邮件中得到{billing\\u field\\u newfield},而不是它的实际值。

有人知道怎么做吗?

谢谢

1 个回复
SO网友:vikrant zilpe

Adding custom fields to emails

/* To use: 
   1. Add this snippet to your theme\'s functions.php file
   2. Change the meta key names in the snippet
   3. Create a custom field in the order post - e.g. key = "Tracking Code" value = abcdefg
   4. When next updating the status, or during any other event which emails 
          the user, they will see this field in their email
 */

add_filter(\'woocommerce_email_order_meta_keys\', \'my_custom_order_meta_keys\');
function my_custom_order_meta_keys( $keys ) {
  $keys[] = \'Tracking Code\'; // This will look for a custom field called \'Tracking Code\' and add it to emails
  return $keys;
}
Woocommerce文档:Customizing checkout fields using actions and filters