The problem seems to be related to missing tax or (and) to applied coupon discount.
The WC_Order_Item_Product
method get_total()
is the discounted line item total without taxes. In this case when a coupon is used, the discount is applied to the item depending of the coupon type and restrictions.
The WC_Order_Item_Product
method get_subtotal()
is the non discounted line item total without taxes.
When there is no discount (no coupon applied), get_total()
and get_subtotal()
have the same value.
So in your case, you may need to add the taxes using one of this WC_Order_Item_Product
methods:
get_total_tax()
method as discounted line item tax totalget_subtotal_tax()
method as non discounted line item tax total
and to sum them with the corresponding total to get the line item total with tax like:
foreach( $order->get_items() as $item ) {
$item_total = $item->get_total() + $item->get_total_tax(); // Discounted total with tax
}
or
foreach( $order->get_items() as $item ) {
$item_total = $item->get_subtotal() + $item->get_subtotal_tax(); // NON discounted total with tax
}
So this time you will get the correct amount.
Related: Get Order items and WC_Order_Item_Product in WooCommerce 3