如何在用户详细信息邮件模板中添加自定义结账字段

时间:2019-10-11 作者:Skafec

我需要将结帐页面中的自定义字段添加到电子邮件模板中。我已经在结账页面添加了自定义输入,它显示在邮件中,但它显示在产品表之后,我希望它显示在客户详细信息中。我找不到合适的钩子把它放在那里。我的自定义字段代码:

// Hook in
add_filter(\'woocommerce_checkout_fields\', \'custom_override_checkout_fields\');

// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields($fields)
{

  //New input
  $fields[\'billing\'][\'OIB\'] = array(
    \'label\'     => __(\'OIB\', \'woocommerce\'),
    \'placeholder\'   => _x(\'OIB\', \'placeholder\', \'woocommerce\'),
    \'required\'  => true,
    \'class\'     => array(\'form-row-wide\'),
    \'clear\'     => true
  );

  return $fields;
}

/**
 * Update the order meta with field value
 */
add_action(\'woocommerce_checkout_update_order_meta\', \'my_custom_checkout_field_update_order_meta\');

function my_custom_checkout_field_update_order_meta($order_id)
{
  if (!empty($_POST[\'OIB\'])) {
    update_post_meta($order_id, \'OIB\', sanitize_text_field($_POST[\'OIB\']));
  }
}


/**
 * Display field value on the order edit page
 */
add_action(\'woocommerce_admin_order_data_after_billing_address\', \'my_custom_checkout_field_display_admin_order_meta\', 10, 1);

function my_custom_checkout_field_display_admin_order_meta($order)
{
  echo \'<p><strong>\' . __(\'OIB\') . \':</strong> \' . get_post_meta($order->id, \'OIB\', true) . \'</p>\';
}


/* 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 = "OIB" value = 134852145798
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[] = \'OIB\'; // This will look for a custom field called \'OIB\' and add it to emails
  return $keys;
}

//Micanje Checkout polja
add_filter(\'woocommerce_billing_fields\', \'bbloomer_move_checkout_email_field\', 10, 1);

function bbloomer_move_checkout_email_field($address_fields)
{
  $address_fields[\'billing_email\'][\'priority\'] = 25;
  $address_fields[\'OIB\'][\'priority\'] = 26;
  $address_fields[\'billing_phone\'][\'priority\'] = 27;
  $address_fields[\'billing_country\'][\'priority\'] = 28;
  return $address_fields;
}

// Custom validacija za OIB
add_action(\'woocommerce_checkout_process\', \'custom_validate_OIB\');
function custom_validate_OIB()
{
  $is_correct = preg_match(\'/^[0-9]{11}$/\', $_POST[\'OIB\']);
  if ($_POST[\'OIB\'] && !$is_correct) {
    wc_add_notice(__(\'OIB se sastoji od <strong>11 brojki</strong>.\'), \'error\');
  }
}
我使用NP Quote Request插件,它有自己的电子邮件模板。我认为,电子邮件模板php文件底部的一个挂钩控制着客户的详细信息。

do_action(\'woocommerce_email_after_order_table\', $order, $sent_to_admin, $plain_text);

do_action(\'woocommerce_email_order_meta\', $order, $sent_to_admin, $plain_text);

do_action(\'woocommerce_email_confirmation_messages\', $order, $sent_to_admin, $plain_text);

do_action(\'woocommerce_email_customer_details\', $order, $sent_to_admin, $plain_text, $email);


try {
    do_action(\'woocommerce_email_footer\');
} catch (Exception $ex) { }
任何我应该瞄准哪一个钩子或我应该做什么的帮助都会很好。谢谢

1 个回复
SO网友:KGreene

使用woocommerce\\u email\\u customer\\u详细信息。

function email_order_user_meta( $order, $sent_to_admin, $plain_text ) {
   $meta = get_post_meta($order->ID, \'OIB\');
   if($meta){
       /*** whatever format you like here ***/
       echo \'<tr>\' . $meta \'</tr>\';
   }
}
add_action(\'woocommerce_email_customer_details\', \'email_order_user_meta\', 30, 3 );