2
votes

I'm helping my mother create her website for her store i Denmark. It has gone okay, but now i'm stuck with a problem. I need to be able to make a custom field in the order complete email, that displays a track & trace number, i've inserted on the admin order page. Everything i've done up until now, haven't worked, so therefore i seek your help.

I've added a custom field already called Track & Trace Pakkenr. (see screenshot 1) But the problem is getting this in the order complete email, under shipping (forsendelse in danish, see screenshot 2)

I am also a complete and utter beginner in coding so if any of you can give any help or advice, please make it almost foolproof to follow.

Here are the screenshot 1 and screenshot 2.

3

3 Answers

5
votes

To get this order custom field value displayed in order totals table on email notifications, use the following:

add_filter( 'woocommerce_get_order_item_totals', 'insert_custom_line_order_item_totals', 10, 3 );
function insert_custom_line_order_item_totals( $total_rows, $order, $tax_display ){
    // Only on emails notifications
    if( is_wc_endpoint_url() ) return $total_rows; // Exit

    $tracking_label = 'Track & Trace Pakkenr.'; // The tracking label name
    $tracking_value = $order->get_meta( $tracking_label ); // Get the tracking value (custom field).

    if( empty($tracking_value) ) return $total_rows; // Exit

    $new_total_rows  = array(); // Initializing

    // Loop through total rows
    foreach( $total_rows as $key => $value ){
        if( 'payment_method' == $key && ! empty($tracking_value) ) {
            $new_total_rows['tracking_parcel'] = array(
                'label' => $tracking_label,
                'value' => $tracking_value,
            );
        }
        $new_total_rows[$key] = $total_rows[$key];
    }

    return sizeof($new_total_rows) > 0 ? $new_total_rows : $total_rows;
}

Code goes in function.php file of your active child theme (or active theme). tested and works.

You will get something like:

enter image description here

From this:

enter image description here

0
votes

You can add custom fields to the order email, you need to use the field key where the code has meta_key.

/**
 * 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;
}

Ref: https://docs.woocommerce.com/document/add-a-custom-field-in-an-order-to-the-emails/

0
votes

Below is a simple yet functional example of adding custom meta fields to order email

add_filter( 'woocommerce_email_order_meta_fields', 'woocommerce_email_order_meta_fields_func', 10, 3 );

function woocommerce_email_order_meta_fields_func( $fields, $sent_to_admin, $order ) {

    $fields['Track_Field'] = array(
        'label' => __( 'Track', 'woocommerce' ),
        'value' => wptexturize( get_post_meta( $order->id, 'Track_Field', true ) )
    );


    //... more meta fields goes here

    return $fields;
}

In email after order email table:

add_action( 'woocommerce_email_after_order_table', 'woocommerce_email_after_order_table_func' );
function woocommerce_email_after_order_table_func( $order ) {
    ?>

    <h3>Track</h3>
    <table>
        <tr>
            <td>Track Fields </td>
            <td><?php echo wptexturize( get_post_meta( $order->id, 'Track_Field', true ) ); ?></td>
        </tr>

        <!--additional custom meta and HTML code goes here-->
    </table>

    <?php
}

For more help see this link : Click here