0

I need to add custom field from my checkout page to email templates. I already added custom input at checkout page and it shows up in mails but it shows up after products table and I would like it to show up in customer details. I can't find right hook to place it there. Code for my custom field:

// 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');
  }
}

I use NP Quote Request plugin which has it's own email templates. I think that one of these hooks at the bottom of email template php file controls customer details.

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) { }

Any help on which hook I should target or what I should do would be great. Thanks.

1 Answer 1

-1

Use woocommerce_email_customer_details.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.