I have created some custom fields on the woocommerce checkout page. My code is correct and the fields display properly I have saved the field data and displayed them in the admin panel. My code is correct and the fields display properly.
I have written code to include this data in the admin email whenever a new product is ordered. My code is NOT correct and the information is not displayed in the email.
All other stackoverflow answers regarding this topic rely on a deprecated filter
woocommerce_email_order_meta_keys
There is no stackoverflow that answers with the woocommerce 3.8.0 woocommerce_email_order_meta_fields
filter.
I am running woocommerce 3.8.0 and wordpress 5.3. I am saving these files in wp-content/themes/child-theme/functions.php
WTF is wrong with my code? I have checked it again and again and I can't figure out what is wrong. Can someone tell me what I am doing wrong? I am a ruby on rails developer trying to teach myself php and wordpress.
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['custom_field_1'] = array(
'label' => __( 'custom_field_1' ),
'value' => get_post_meta( $order->id, 'custom_field_1', true ),
);
$fields['custom_field_2'] = array(
'label' => __( 'custom_field_2' ),
'value' => get_post_meta( $order->id, 'custom_field_2', true ),
);
$fields['custom_field_3'] = array(
'label' => __( 'custom_field_3' ),
'value' => get_post_meta( $order->id, 'custom_field_3', true ),
);
$fields['custom_field_4'] = array(
'label' => __( 'custom_field_4' ),
'value' => get_post_meta( $order->id, 'custom_field_4', true ),
);
return $fields;
}
My custom form fields in the woocommerce checkout page is here
add_filter( 'woocommerce_checkout_fields', 'isca_custom_checkout_fields' );`
function isca_custom_checkout_fields($fields){
$fields['isca_extra_fields'] = array(
'custom_field_1' => array(
'class' => array(
'form-row-first'
),
'type' => 'text',
'required' => true,
'placeholder' => __( 'Name' )
),
'custom_field_2' => array(
'class' => array(
'form-row-last'
),
'type' => 'text',
'required' => true,
'placeholder' => __( 'Nickname' )
),
'custom_field_3' => array(
'class' => array(
'form-row-first'
),
'type' => 'text',
'required' => true,
'placeholder' => __( 'Favorite Exercise' )
),
'custom_field_4' => array(
'class' => array(
'form-row-last'
),
'type' => 'text',
'required' => false,
'placeholder' => __( 'Favorite Stretch' )
),
);
return $fields;
}
I then add it to the woocomerce checkout page with this code
add_action( 'woocommerce_after_checkout_billing_form' ,'isca_extra_checkout_fields' );
function isca_extra_checkout_fields(){
$checkout = WC()->checkout(); ?>
<br/>
<div class="extra-fields">
<h3><?php _e( 'Fitness Information' ); ?></h3>
<?php
foreach ( $checkout->checkout_fields['isca_extra_fields'] as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>
</div>
<?php }
WordPress has an option to turn on debug mode, so that errors and warnings can be identified and resolved. Errors will be logged in a file named debug.log
in the wp-content
folder of the WordPress folder while your code make any errors. So while developing in WordPress, you have to turn on three options.
Goto wp-config.php and add these three lines of code
define('WP_DEBUG', true);
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', true );
your custom_woocommerce_email_order_meta_field
s function has an error. You called order id incorrectly. The error log will show that. Order properties shouldn't be called directly. So you have to change $order->id
to $order->get_id()
. So change the function to
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
if( !$sent_to_admin ){
return;
}
$fields['custom_field_1'] = array(
'label' => __( 'custom field 1' ),
'value' => get_post_meta( $order->get_id(), 'custom_field_1', true ),
);
$fields['custom_field_2'] = array(
'label' => __( 'custom field 2' ),
'value' => get_post_meta( $order->get_id(), 'custom_field_2', true ),
);
$fields['custom_field_3'] = array(
'label' => __( 'custom field 3' ),
'value' => get_post_meta( $order->get_id(), 'custom_field_3', true ),
);
$fields['custom_field_4'] = array(
'label' => __( 'custom field 4' ),
'value' => get_post_meta( $order->get_id(), 'custom_field_4', true ),
);
return $fields;
}
Also you haven't written any code to save the custom fields you added to checkout page. You are trying to find the values that aren't saved in the order meta. You need to write the code to save the custom added checkout fields to the order meta as below.
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 ($_POST['custom_field_1']) update_post_meta( $order_id, 'custom_field_1', esc_attr($_POST['custom_field_1']));
if ($_POST['custom_field_2']) update_post_meta( $order_id, 'custom_field_2', esc_attr($_POST['custom_field_2']));
if ($_POST['custom_field_3']) update_post_meta( $order_id, 'custom_field_3', esc_attr($_POST['custom_field_3']));
if ($_POST['custom_field_4']) update_post_meta( $order_id, 'custom_field_4', esc_attr($_POST['custom_field_4']));
}
Done . . . Now everything is added perfectly to the email (tested and confirmed). Also since you have said these fields are shown in the admin emails, you have to check that condition using
if( !$sent_to_admin ){
return;
}
which i have added in the custom_woocommerce_email_order_meta_fields
function.