Woocommerce Admin-Order, round Problems

  • Unknown's avatar

    Hello Together

    I use WooCommerce and have already made some adjustments. Currently the prices and VAT, especially the total prices in the online shop are rounded to 0.05 digits each. I do this with the PHP function “ceil”:

    add_filter( 'woocommerce_calculated_total', 'custom_calculated_total' );
    function custom_calculated_total( $total ) {
    $total = ceil ( $total / 0.05 ) * 0.05;
    return $total;
    }
    
    add_filter('woocommerce_calculated_subtotal', 'custom_subcalculated_total');
    function custom_subcalculated_total( $total ) {
    $total = ceil ( $total / 0.05 ) * 0.05;
    return $total;
    }

    What I notice is that if I enter a voucher with 5% discount, it shows me again e.g. 9.41 instead of 9.40 or if I enter a value of 10.-, it shows 10.01 instead of 10.-.

    But what I really care about is the rounding in the manual order entry.
    If we want to edit existing orders in the backend (e.g. add a new product, add shipping, change quantity, etc.) the amounts, VAT, total will be recalculated. But here everything in wild form like 105.27 or 210.79. How do I get these amounts to be rounded as well? Otherwise it looks very unattractive! If you click on “Recalculate”, the unrounded and unattractive amounts are also included in the new calculation.

    I tried this with this function in “functions.php”, but without success.

           function calculate_totals( $and_taxes = true ) {
        do_action( 'woocommerce_order_before_calculate_totals', $and_taxes, $order );
    
        $fees_total        = 0;
        $shipping_total    = 0;
        $cart_subtotal_tax = 0;
        $cart_total_tax    = 0;
    
        $cart_subtotal = $order->get_cart_subtotal_for_order();
        $cart_total    = $order->get_cart_total_for_order();
    
        // Sum shipping costs.
        foreach ( $order->get_shipping_methods() as $shipping ) {
            $shipping_total += round( $shipping->get_total(), wc_get_price_decimals() );
        }
    
        $order->set_shipping_total( $shipping_total );
    
        // Sum fee costs.
        foreach ( $order->get_fees() as $item ) {
            $fee_total = $item->get_total();
    
            if ( 0 > $fee_total ) {
                $max_discount = round( $cart_total + $fees_total + $shipping_total, wc_get_price_decimals() ) * -1;
    
                if ( $fee_total < $max_discount && 0 > $max_discount ) {
                    $item->set_total( $max_discount );
                }
            }
            $fees_total += $item->get_total();
        }
    
        // Calculate taxes for items, shipping, discounts. Note; this also triggers save().
        if ( $and_taxes ) {
            $order->calculate_taxes();
        }
    
        // Sum taxes again so we can work out how much tax was discounted. This uses original values, not those possibly rounded to 2dp.
        foreach ( $order->get_items() as $item ) {
            $taxes = $item->get_taxes();
    
            foreach ( $taxes['total'] as $tax_rate_id => $tax ) {
                $cart_total_tax += (float) $tax;
            }
    
            foreach ( $taxes['subtotal'] as $tax_rate_id => $tax ) {
                $cart_subtotal_tax += (float) $tax;
            }
        }
    
        $order->set_discount_total( ceil( $cart_subtotal - $cart_total, wc_get_price_decimals()
     / 0.05) * 0.05 );
        $order->set_discount_tax( wc_round_tax_total( $cart_subtotal_tax - $cart_total_tax ) );
        $order->set_total( ceil($cart_total / 0.05) * 0.05 + ceil($fees_total / 0.05) * 0.05 + ceil($order->get_shipping_total() / 0.05) * 0.05 + ceil($order->get_cart_tax() / 0.05) * 0.05 + $order->get_shipping_tax(), wc_get_price_decimals() ) );
    
        do_action( 'woocommerce_order_after_calculate_totals', $and_taxes, $order );
    
        $order->save();
    
        return $order->get_total();
    }

    I’m out of ideas myself, can anyone help me?
    Here at the picture, the red circled should be rounded to 0.05. So either up or down.

  • Hi there,

    You’re in the wrong forum. To get help with the WooCommerce plugin you need to post in these forums:

    https://wordpress.org/support/plugin/woocommerce

    The forums you are currently posting are for providing support to WordPress.com hosted sites only.

  • The topic ‘Woocommerce Admin-Order, round Problems’ is closed to new replies.