Woocommerce - Display full price (incl. tax) in admin order summary

Viewed 544

I wish to display the full price that the customer paid for the item, rather than 'price without tax' and 'tax'.

I followed this: Woocommerce - Admin Order Detail Page and it almost gets me what I want, however it is not perfect to my case and I am failing to modify it. I am not very experienced in PHP and worried about breaking anything as it is a live webshop.

I managed to change the currency sign easily enough and put it at the end of the price rather than the start, but there's a couple of other things I wish to change and I'm not sure how/where.

  1. We use commas instead of periods as decimal points. This is reflected without a problem everywhere else, but not with this custom snippet. How do I change this?

  2. In the case of free item/shipping, this is empty. I tried to change "if $val>0" to just "if $val" but it made no difference.

  3. I'd like this entire column to be on the right (as the last column) rather than on the left. So it reads Quantity, Total, Tax, Total incl. Tax.

Here is my current, slightly modified code:

function action_woocommerce_admin_order_item_values( $null, $item, $absint ) {
    $val = ($item['type'] == 'line_item' || $item['type'] == 'shipping') ? $item['total'] + $item['total_tax'] : ' ';
    $valdecimal = wc_format_decimal( $val, $dp='', $trim_zeros );
    ?>
    <td class="item_fcost" data-sort-value="<?php echo $val; ?>">
        <div class="view" style="font-weight: bold; text-align: right; padding-right: 10px;">
            <?php if ($val) echo $valdecimal; echo ' DKK'; ?>
        </div>
    </td>
    <?php
};
add_action( 'woocommerce_admin_order_item_values', 'action_woocommerce_admin_order_item_values', 10, 3 );


function action_woocommerce_admin_order_item_headers( $order ) {
    echo '<th class="item_fcost sortable" data-sort="float" style="text-align: right;">Pris inkl. moms</th>';
};
add_action( 'woocommerce_admin_order_item_headers', 'action_woocommerce_admin_order_item_headers', 10, 3 );

Edit: I found out that this code also breaks/throws a fatal error with refunded orders. I've had to deactivate the code entirely.

I'm not experienced enough to know how to modify it and am still looking for help.

1 Answers

There was a little problem in your code and that's that you were accesing the $item as it was an array while it's actually an object. One of the objects extending the WC_Order_Item abstract classes. Also, the total and total_tax properties of $item are strings, not numbers so when you are adding them together, you first need to convert them to floats.

Lastly, I reasearched how the total is displayed by Woocommerce and they include the refunded total too. So I added the refunded total including VAT/tax too.

You can find updated code below.

function action_woocommerce_admin_order_item_values( $null, WC_Order_Item $item) {
    $val = floatval($item->get_total()) + floatval($item->get_total_tax());
    ?>
    <td class="item_full_cost" data-sort-value="<?= $val ?>">
        <div class="view" style="font-weight: bold; text-align: right; padding-right: 10px;">
            <?= wc_price($val, ['currency' => $item->get_order()->get_currency()]) ?>
        </div>
        <?php
        $refunded = $item->get_order()->get_total_refunded_for_item( $item->get_id() );

        if ( $refunded ) {
            echo '<small class="refunded">-' . wc_price( $refunded, array( 'currency' => $item->get_order()->get_currency() ) ) . '</small>';
        }
        ?>
    </td>
    <?php
};
add_action( 'woocommerce_admin_order_item_values', 'action_woocommerce_admin_order_item_values', 10, 2 );


function action_woocommerce_admin_order_item_headers( $order ) {
    echo '<th class="line_full_cost sortable" data-sort="float" style="font-weight:bold;text-align: right;">' . __('Price incl. VAT') .'</th>';
};
add_action( 'woocommerce_admin_order_item_headers', 'action_woocommerce_admin_order_item_headers', 10, 3 );
Related