Add shipping_tax_amount into a tax calculation? Woocommerce/PHP

I am trying to add the “shipping_tax_amount” ontop of the normal tax calculation for a product sale report in woocommerce.

Here is the original snippet of code and then here is my modifications. I’m not sure where I went wrong.

I appreciate your help!!

Original:

<?php
if ( wc_tax_enabled() ) :
$line_tax_data = isset( $item['line_tax_data'] ) ? $item['line_tax_data'] : '';
$tax_data      = maybe_unserialize( $line_tax_data );

foreach ( $order_taxes as $tax_item ) :
$tax_item_id       = $tax_item['rate_id'];
$tax_item_total    = isset( $tax_data['total'][ $tax_item_id ] ) ? $tax_data['total'][ $tax_item_id ] : '';
$tax_item_subtotal = isset( $tax_data['subtotal'][ $tax_item_id ] ) ? $tax_data['subtotal'][ $tax_item_id ] : '';
?>
<td class="line_tax" width="1%">
<div class="view">
<?php
if ( '' != $tax_item_total ) {
if ( isset( $tax_item_subtotal ) && $tax_item_subtotal != $tax_item_total ) {
    echo '<del>' . wc_price( wc_round_tax_total( $tax_item_subtotal ), array( 'currency' => $order->get_order_currency() ) ) . '</del> ';
    }


    echo wc_price( wc_round_tax_total( $tax_item_total ), array( 'currency' => $order->get_order_currency() ) );
    $tax_row_2 = wc_price( wc_round_tax_total( $tax_item_total ), array( 'currency' => $order->get_order_currency() ) );
    } else {
    echo '&ndash;';
    }
    ?>
    </div>

My Modifification with $sql line and + $shipping_tax_amount in the echo wc_price line: Lines 5 and 21.

<?php
if ( wc_tax_enabled() ) :
$line_tax_data = isset( $item['line_tax_data'] ) ? $item['line_tax_data'] : '';
$tax_data      = maybe_unserialize( $line_tax_data );
$sql = "SELECT * FROM $wpdb->woocommerce_order_itemmeta WHERE meta_key = 'shipping_tax_amount' AND meta_value='$shipping_tax_amount'";

foreach ( $order_taxes as $tax_item ) :
$tax_item_id       = $tax_item['rate_id'];
$tax_item_total    = isset( $tax_data['total'][ $tax_item_id ] ) ? $tax_data['total'][ $tax_item_id ] : '';
$tax_item_subtotal = isset( $tax_data['subtotal'][ $tax_item_id ] ) ? $tax_data['subtotal'][ $tax_item_id ] : '';
?>
<td class="line_tax" width="1%">
<div class="view">
<?php
if ( '' != $tax_item_total ) {
if ( isset( $tax_item_subtotal ) && $tax_item_subtotal != $tax_item_total ) {
    echo '<del>' . wc_price( wc_round_tax_total( $tax_item_subtotal ), array( 'currency' => $order->get_order_currency() ) ) . '</del> ';
    }


    echo wc_price( wc_round_tax_total( $tax_item_total + $shipping_tax_amount ), array( 'currency' => $order->get_order_currency() ) );
    $tax_row_2 = wc_price( wc_round_tax_total( $tax_item_total ), array( 'currency' => $order->get_order_currency() ) );
    } else {
    echo '&ndash;';
    }
    ?>
    </div>

Read more here: Add shipping_tax_amount into a tax calculation? Woocommerce/PHP

Leave a Reply

Your email address will not be published. Required fields are marked *