'Warning: Division by zero - Wordpress

I am trying to show the price with VAT included in woocommerce orders backoffice but i am getting this error when i try to divide the total by the quantity.

The total amount it shows without any problem, but if i add the division gives me this error. Any help with this?

function action_woocommerce_admin_order_item_values( $null, $item, $absint ) {

    $val = ($item['type'] == 'line_item' || $item['type'] == 'shipping' ) ? ($item['total'] + $item['total_tax']) / $item['quantity'] : ' ';
    $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>0) echo '€'; echo $valdecimal;?>
        </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;">Preço com IVA</th>';
};
add_action( 'woocommerce_admin_order_item_headers', 'action_woocommerce_admin_order_item_headers', 10, 3 );


Solution 1:[1]

SOLVED

$quantity = $item->get_quantity();

$val = ($item['type'] == 'line_item' || $item['type'] == 'shipping' ) ? ($item['total'] + $item['total_tax']) / $quantity : '&nbsp;';

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DiogoCarvalho