phptemplateswoocommerceordersemail-notifications

Add a Unit price column to order items in WooCommerce Email Notifications


I need Unit Price of the Product to be shown in a separate column in my email notifications. I have managed to do it for Cart and Checkout pages.

I am currently using WC 9.8.5. Many of related answer codes from Stack overflow, were designed to work with WooCommerce 3 (more than 6 years ago).

I guess that we need some update for the same function that fellow users have been asking previously.

Please see current screenshot of a test order :

enter image description here

I managed to add the <th> HTML tag in email-order-details.php template. But couldn't get the "price data" displayed at all.

I was using this code line:

wc_price( $order->get_item_total( $item, true, true ), array( 'currency' => $order->get_order_currency() ) );  

But it don't work anymore, neither some other code attempts I tried.

This is the coding right after Product name :

        <td class="td font-family text-align-<?php echo esc_attr( $price_text_align ); ?>" style="vertical-align:middle;;">
            <?php echo $email_improvements_enabled ? '&times;' : '';
            // Added U.price before Qty
            wc_price( $order->get_item_total( $item, true, true ), array( 'currency' => $order->get_order_currency() ) );  
            ?>
        </td>
        <td class="td font-family text-align-<?php echo esc_attr( $price_text_align ); ?>" style="vertical-align:middle;">
            <?php echo $email_improvements_enabled ? '&times;' : '';
            // Qty is original before Amt
            $qty          = $item->get_quantity();
            $refunded_qty = $order->get_qty_refunded_for_item( $item_id );

            if ( $refunded_qty ) {
                $qty_display = '<del>' . esc_html( $qty ) . '</del> <ins>' . esc_html( $qty - ( $refunded_qty * -1 ) ) . '</ins>';
            } else {
                $qty_display = esc_html( $qty );
            }
            echo wp_kses_post( apply_filters( 'woocommerce_email_order_item_quantity', $qty_display, $item ) ); 
            ?>
        </td>
        <td class="td font-family text-align-<?php echo esc_attr( $price_text_align ); ?>" style="vertical-align:middle;">
            <?php echo wp_kses_post( $order->get_formatted_line_subtotal( $item ) ); ?>
        </td>
    </tr>
    <?php

    if ( $show_purchase_note && $purchase_note ) {
        ?>
        <tr>
            <td colspan="3" class="font-family text-align-left" style="vertical-align:middle;">
                <?php
                echo wp_kses_post( wpautop( do_shortcode( $purchase_note ) ) );
                ?>
            </td>
        </tr>
        <?php
    }
    ?>

Solution

  • Sorry, but WC_Abstarct_Order get_item_total() method is still valid in last WooCommerce version and works just fine.

    The mistake in your code is that you forgot to add echo in that related code line, so that is why nothing is displayed.

    So replace the following code block:

    <?php echo $email_improvements_enabled ? '&times;' : '';
        // Added U.price before Qty
        wc_price( $order->get_item_total( $item, true, true ), array( 'currency' => $order->get_order_currency() ) );  
    ?>
    

    with:

    <?php
    // Added U.price before Qty
    echo wc_price( $order->get_item_total( $item, true, true ), array( 'currency' => $order->get_order_currency() ) );  
    ?>
    

    It should work.

    Note that echo $email_improvements_enabled ? '&times;' : ''; is not needed in your additional code as it already exist after.