Support » Plugin: WooCommerce » Add custom field to “customer details” block in order email

  • Resolved KS

    (@karl19)


    We have added a “gift card comments” custom field on WooCommerce checkout, which we then show on the order details page by adding it to /order/order-details-customer.php This works fine, it looks like this:

    Customer details
    Delivery Instructions: Some instructions
    Gift Card Comments: Some comments
    Email: customer@gmail.com
    Telephone: 012345

    We have trouble getting the same output in the order email. If we add this to functions.php:

    function custom_gift_card__order_meta_keys( $keys ) {
         $keys[] = 'Gift Card Comments';
         return $keys;
    }
    add_filter('woocommerce_email_order_meta_keys', 'custom_gift_card__order_meta_keys');

    it gets added to above the customer details block. We have tried hooking into:

    woocommerce_email_customer_details

    but don’t get any output. Ideally we would like to:

    1. Get gift card comments in second place, just as on checkout – but would be happy to get it anywhere in the customer details block.
    2. Rename “Note” label in email to “Delivery Instructions”.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter KS

    (@karl19)

    We got this working, by filtering on “woocommerce_email_customer_details_fields” instead of “woocommerce_email_order_meta_keys” and adding a $fields array holding the data:

    function custom_gift_card__order_meta_keys($fields, $sent_to_admin, $order ) {
    	$fields['gift_card_comments'] = array(
    	'label' => __('Gift Card Comments', 'woocommerce'),
    	'placeholder' => _x('', 'placeholder', 'woocommerce'),
    	'required' => true,
    	'class' => array('form-row-last'),
    	'clear' => true
    	);
    	$fields["gift_card_comments"]["value"] = get_post_meta( $order->id, 'Gift Card Comments', true );
    	return $fields;
    }
    add_filter( 'woocommerce_email_customer_details_fields', 'custom_gift_card__order_meta_keys', 40, 3);

    I tried this, but it didn’t work. Were there additional changes?

    Thread Starter KS

    (@karl19)

    @tendigit, sorry about the late reply on this, I have been on parental leave and I missed many notifications.

    Perhaps you have already solved this, but as far as I can remember, there was nothing else to it. The important part would be to make sure to update the code with the correct custom field value.

    This worked for me (with my own fields listed) based on your response. The key has to match what’s in your custom field, so it’s not the name with underscores (first_time_member as one of my examples). If you include special characters in the key, like a quotation mark, be sure to escape it.

    /**
     * Add custom fields to emails
     */
    add_filter('woocommerce_email_customer_details_fields', 'my_checkout_field_order_meta_fields', 40, 3 );
    function my_checkout_field_order_meta_fields( $fields, $sent_to_admin, $order ) {
      $fields['first_time_member'] = array(
        'label' => __( 'First Time Member?' ),
        'value' => get_post_meta( $order->id, 'First Time Member?', true ),
      );
      $fields['spouse_name'] = array(
        'label' => __( 'Spouse Name' ),
        'value' => get_post_meta( $order->id, 'Spouse Name', true ),
      );
      $fields['childrens_ages'] = array(
        'label' => __( 'Children\'s Ages' ),
        'value' => get_post_meta( $order->id, 'Children\'s Ages', true ),
      );
      return $fields;
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Add custom field to “customer details” block in order email’ is closed to new replies.