Search code examples
phptemplateswoocommerceordersemail-notifications

Add a column to show order item price excl. tax in Woocommerce email notifications


I have been trying to find a way to add the price excluding taxes for each item in an additional column on the new order email notification.

This is what is currently shown:

Current customer message

and this is what I'm looking for:

Desired Customer message

The current email template is this

/*
 * @hooked WC_Emails::email_header() Output the email header
 */
do_action( 'woocommerce_email_header', $email_heading, $email ); ?>

<?php /* translators: %s: Customer billing full name */ ?>
<p><?php printf( esc_html__( 'You’ve received the following order from %s:', 'woocommerce' ), $order->get_formatted_billing_full_name() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></p>
<?php

/*
 * @hooked WC_Emails::order_details() Shows the order details table.
 * @hooked WC_Structured_Data::generate_order_data() Generates structured data.
 * @hooked WC_Structured_Data::output_structured_data() Outputs structured data.
 * @since 2.5.0
 */
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

/*
 * @hooked WC_Emails::order_meta() Shows order meta data.
 */
do_action( 'woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text, $email );

/*
 * @hooked WC_Emails::customer_details() Shows customer details
 * @hooked WC_Emails::email_address() Shows email address
 */
do_action( 'woocommerce_email_customer_details', $order, $sent_to_admin, $plain_text, $email );

/**
 * Show user-defined additional content - this is set in each email's settings.
 */
if ( $additional_content ) {
    echo wp_kses_post( wpautop( wptexturize( $additional_content ) ) );
}

/*
 * @hooked WC_Emails::email_footer() Output the email footer
 */
do_action( 'woocommerce_email_footer', $email );

As you can see I don't have a custom email template I have managed to add a row excluding the tax on the total using a source filter but I've been unable to adapt this into a column.

//Adding excluding tax row to order email
add_filter( 'woocommerce_get_order_item_totals', 'add_order_total_excl_vat_row', 10, 3 );
function add_order_total_excl_vat_row( $total_rows, $order, $tax_display ) {
    if( ! is_wc_endpoint_url() || ! is_admin() ) {

        // Set last total row in a variable and remove it.
        $gran_total = $total_rows['order_total'];
        unset( $total_rows['order_total'] );

        // Insert our new row
        $total_rows['order_total_ev'] = array(
            'label' => __( 'Total Excl. VAT :', 'woocommerce' ),
            'value' => wc_price( $order->get_total() - $order->get_total_tax() ),
        );

        // Set back last total row
        $total_rows['order_total'] = $gran_total;
    }
    return $total_rows;
}

I've been trying to find a fix but almost all fixes I've seen require a custom template and I am hoping to avoid that so as not needing to do as much maintanace when upgrades come through. Most of my attempts to have any change have resulted in errors of some kind or another.


Solution

  • To add the price excluding taxes for each item in an additional column on email notifications, it can only be done by overriding WooCommerce templates via your child theme.

    But you can't target a specific email notification, like "New Order", as this will also affect customer email notifications.

    The templates involved are located in WooCommerce plugin inside the "templates" folder:

    • emails/email-order-details.php
    • emails/email-order-items.php

    You need to copy those 2 template files in your child theme, inside a "woocommerce" folder like:

    • yourchildtheme/woocommerce/emails/email-order-details.php
    • yourchildtheme/woocommerce/emails/email-order-items.php

    1) Open/edit copied email-order-details.php template file:

    Step 1 - Replace line 72:

    <td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo wp_kses_post( $total['value'] ); ?></td>
    

    with:

    <td class="td" colspan="2" style="text-align:<?php echo esc_attr( $text_align ); ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo wp_kses_post( $total['value'] ); ?></td>
    

    Step 2 - Replace line 81:

    <td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php echo wp_kses_post( nl2br( wptexturize( $order->get_customer_note() ) ) ); ?></td>       
    

    with:

    <td class="td" colspan="2" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php echo wp_kses_post( nl2br( wptexturize( $order->get_customer_note() ) ) ); ?></td>
    

    Step 3 - Insert after line 43:

    <th class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e( 'Price excl. VAT', 'woocommerce' ); ?></th>
    

    Save changes.


    2) Open/edit copied email-order-items.php template file:

    Insert after line 84:

            <td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; vertical-align:middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;">
                <?php echo wp_kses_post( $order->get_formatted_line_subtotal( $item, 'excl' ) ); ?>
            </td>
    

    Save change.


    You are done.

    You will get something like:

    enter image description here