-
-
Save woogists/1b71d6842960fa056bd61bd0ca60ecba to your computer and use it in GitHub Desktop.
/** | |
* Add a custom field (in an order) to the emails | |
*/ | |
add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 ); | |
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) { | |
$fields['meta_key'] = array( | |
'label' => __( 'Label' ), | |
'value' => get_post_meta( $order->id, 'meta_key', true ), | |
); | |
return $fields; | |
} |
So would it look like this?
/** * Add a custom field (in an order) to the emails */ add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 ); function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) { $fields['meta_key'] = array( 'label' => __( 'MY CUSTOM FIELD' ), 'value' => get_post_meta( $order->id, 'MY_CUSTOM_FIELD', true ), ); return $fields; }
Would be nice if we got an answer for this.
Indeed!
Howdy,
The meta_key
needs to match. So like this:
/**
* Add a custom field (in an order) to the emails
*/
add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields['MY_CUSTOM_FIELD'] = array(
'label' => __( 'MY CUSTOM FIELD' ),
'value' => get_post_meta( $order->id, 'MY_CUSTOM_FIELD', true ),
);
return $fields;
}
Great! Do you know where this code has to be placed? Do we need to create a new .php and place it in the child theme folder? Or does it need to be added to another existing file?
Thanks
Ok, I found the solution it needs to be in functions.php, paste it to the end of the file.
Hi,
Ok, I found the solution it needs to be in functions.php, paste it to the end of the file.
Correct. Cheers for adding that.
I'm pretty green with PHP, but I know JS, when it comes to multiple extra fields, should I just copy, paste and rename the function i.e. custom_woocommerce_email_order_meta_fields
or is there a loop function I can use. thanks!
What would this code look like if you had multiple fields to add?
What would this code look like if you had multiple fields to add?
From what I see there is a foreach
loop that processes the filter so you can pass multiple arrays.
/**
* Add a custom field (in an order) to the emails
*/
add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields['meta_key_1'] = array(
'label' => __( 'Label' ),
'value' => get_post_meta( $order->id, 'meta_key_1', true ),
);
$fields['meta_key_2'] = array(
'label' => __( 'Label' ),
'value' => get_post_meta( $order->id, 'meta_key_2', true ),
);
return $fields;
}
Code is untested 🤞.
I think this should also be updated for CRUD, which would be anything over WooCommerce 3.0. I've also added internationalization.
/**
* Add a custom field (in an order) to the emails
*
* @param array $fields The meta fields.
* @param bool $sent_to_admin If should sent to admin.
* @param WC_Order $order Order instance.
* @return array
*/
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields['meta_key_1'] = array(
'label' => esc_html__( 'Meta Label 1', 'your-text-domain' ),
'value' => $order->get_meta( 'meta_key_1', true ),
);
$fields['meta_key_2'] = array(
'label' => esc_html__( 'Meta Label 2', 'your-text-domain' ),
'value' => $order->get_meta( 'meta_key_2', true ),
);
return $fields;
}
add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
👍 💌
Thanks a lot @helgatheviking 🙂!
Has anyone actually verified this piece of code?
Do you know how to move the field in the e-mail? Like I want it to be after the billing details and add some style
instead of just those fields?
@marketingsolution I have a snippet in my tutorial: https://www.kathyisawesome.com/woocommerce-customize-checkout-fields/
This should display fields on the woocommerce_email_customer_details
hook. You can check the email templates for available hooks.
function kia_display_email_order_meta( $order, $sent_to_admin, $plain_text ) {
$some_field = $order->get_meta( '_some_field' );
$another_field = $order->get_meta( '_another_field' );
if( $plain_text ){
echo 'The value for some field is ' . $some_field . ' while the value of another field is ' . $another_field;
} else {
echo 'The value for <strong>some field</strong> is ' . $some_field. ' while the value of <strong>another field</strong> is ' . $another_field . ';
}
}
add_action('woocommerce_email_customer_details', 'kia_display_email_order_meta', 30, 3 );
Thank you I dont know for what we are using this else { } ?
I try to adapt it but've got critical error on page, please take a look, want to show just one field txt:
`function NIP_display_email_order_meta( $order, $sent_to_admin, $plain_text ) {
$NIP = $order->get_meta( '_NIP' );
if( $plain_text ){
echo 'Podany NIP to ' . $NIP;
} else {
echo 'The value for <strong>some field</strong> is ' . $NIP.;
}
}
add_action('woocommerce_email_customer_details', 'NIP_display_email_order_meta', 30, 3 );
`
@marketingsolution the if/else is for emails that are sent in plain text versus emails that are sent with HTML markup. You didn't say what your fatal error was so it's hard to say... though I do see a problem at the end of echo 'The value for <strong>some field</strong> is ' . $NIP.;
there's a period just before the semi-colon that should not be there.
function NIP_display_email_order_meta( $order, $sent_to_admin, $plain_text ) {
$NIP = $order->get_meta( '_NIP' );
if( $plain_text ){
echo 'Podany NIP to ' . $NIP;
} else {
echo 'The value for <strong>some field</strong> is ' . $NIP;
}
}
add_action('woocommerce_email_customer_details', 'NIP_display_email_order_meta', 30, 3 );
So would it look like this?