• Hello,
    I would WC to calculate all prices rounded by 2 digits, not by 6 (default or more).

    Case: I have regular and B2B price shop in one. The default setting is that shop is price includes taxes, howerver in B2B it doesn’t include taxes. Therefore, I have a product with regular price & taxes 29,99e, and 19,16e without (19% DE VAT). If I add 12 items it calculates – 19.957999€ * 12 = 239.495800e -> 239.50e, which is actually accurate but I need something else for the end-client.

    I want that if the price is displayed in cart 19,16e and add quantity 12 = 19,16 x 12 = 239,52e. 2 cents difference. Same applies to EVERYTHING else in cart => discounts, fees, shipping, subtotal, total.

    Shortly, if the single price is rounded by 2 digits, then all other calculations are rounded by 2 as well, instead of the super précised calculation (6). What you see is what you get ??

    I tried:

    //add_filter('raw_woocommerce_price', 'round_price_product', 1000, 1);
    
    add_filter('woocommerce_product_get_price', 'round_price_product', 1000, 1);
    
    add_filter('woocommerce_product_get_regular_price', 'round_price_product', 1000, 1);
    
    add_filter('woocommerce_product_variation_get_price', 'round_price_product', 1000, 1);
    
    add_filter('woocommerce_product_variation_get_regular_price', 'round_price_product', 1000, 1);
    
    add_filter('woocommerce_get_price_excluding_tax', 'round_price_product', 1000, 1);
    
    add_filter('woocommerce_get_price_including_tax', 'round_price_product', 1000, 1);
    
    //add_filter('woocommerce_tax_round', 'round_price_product', 1000, 1);
    
    function round_price_product($price) {
    
        // Return rounded price
    
        return round($price, 2);
    
    }

    I also tried to modify the main WC_ROUNDING_PRECISION but obviously it won’t work.

    The only way I found which is very bad IMO is to manipulate the cart manually by calculating every single line via woocommerce_cart_product_subtotal and woocommerce_after_calculate_totals. However, I prefer a more elegant way if possible.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Alexander Georgiev

    (@ageorgiev)

    Ok, I manage to do it with the 2 filters. If anyone is interested:

    $single_price ? ? ? ?= wc_get_price_excluding_tax($product, array(‘qty’ => 1));? ? $row_price = (float) $single_price * (float) $quantity;

    function crew_b2b_prices_rounding($price, $qty, $product) {
        $price = number_format($price, wc_get_price_decimals()); //returned format must be 0,08. No extra digits after.
        return $price;
    }
    add_filter('woocommerce_get_price_excluding_tax', 'crew_b2b_prices_rounding', 10, 3);
    
    function subtotal() {function crew_b2b_product_subtotal_rounding($product_subtotal, $product, $quantity) {
    if ($product->is_taxable()) {
    if (!WC()->cart->display_prices_including_tax()) {
    $single_price        = wc_get_price_excluding_tax($product, array('qty' => 1));
                $row_price = (float) $single_price * (float) $quantity;
                $product_subtotal = wc_price($row_price);
    }
    }
    }
    add_filter('woocommerce_cart_product_subtotal', 'crew_b2b_product_subtotal_rounding', 10, 3);
    
    function crew_b2b_cart_totals_rounding($cart) {
    function crew_b2b_prices_rounding($price, $qty, $product) {
        $price = number_format($price, wc_get_price_decimals()); //returned format must be 0,08. No extra digits after.
        return $price;
    }
    add_filter('woocommerce_get_price_excluding_tax', 'crew_b2b_prices_rounding', 10, 3);
    
    function subtotal() {function crew_b2b_product_subtotal_rounding($product_subtotal, $product, $quantity) {
    if ($product->is_taxable()) {
    if (!WC()->cart->display_prices_including_tax()) {
    $single_price        = wc_get_price_excluding_tax($product, array('qty' => 1));
                $row_price = (float) $single_price * (float) $quantity;
                $product_subtotal = wc_price($row_price);
    }
    }
    }
    add_filter('woocommerce_cart_product_subtotal', 'crew_b2b_product_subtotal_rounding', 10, 3);
    
    function crew_b2b_cart_totals_rounding($cart) {
        if (!$cart->display_prices_including_tax()) {
    
    
            $subtotal = 0;
            $taxes = 0;
            $tax = new WC_Tax();
    
            $price_includes_tax = false;
            $product_taxes = array();
    
            foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
              
                $product_subtotal = 0;
    
                $product_id = $cart_item['product_id'];
                $product = wc_get_product($product_id);
                $single_price        = wc_get_price_excluding_tax($product, array('qty' => 1));
                $quantity = $cart_item['quantity'];
                $product_subtotal += $single_price * $quantity;
                $cart->cart_contents[$cart_item_key]['line_subtotal'] = $product_subtotal;
                $cart->cart_contents[$cart_item_key]['line_total'] = $product_subtotal;
                if ($product->is_taxable()) {
    
                    $tax_rates = $tax->get_rates($product->get_tax_class());
                    $subtotal_taxes = WC_Tax::calc_tax($product_subtotal, $tax_rates, $price_includes_tax);
    
                    $subtotal_tax = array_sum($subtotal_taxes);
                    $cart->cart_contents[$cart_item_key]['line_tax_data']['subtotal'] = $subtotal_taxes;
                    $cart->cart_contents[$cart_item_key]['line_tax_data']['total'] = $subtotal_taxes;
                    $cart->cart_contents[$cart_item_key]['line_subtotal_tax'] = number_format($subtotal_tax, wc_get_price_decimals());
                    $cart->cart_contents[$cart_item_key]['line_tax'] = number_format($subtotal_tax, wc_get_price_decimals());
                    $taxes += $cart->cart_contents[$cart_item_key]['line_subtotal_tax'];
    
                    //set product taxes here
                    foreach ($subtotal_taxes as $key => $value) {
                        if (isset($product_taxes[$key])) {
                            $product_taxes[$key] += $value; // Add to existing key
                        } else {
                            $product_taxes[$key] = $value; // Create new key
                        }
                    }
                }
                $subtotal += $cart->cart_contents[$cart_item_key]['line_subtotal'];           
            }
           
            $fees = $cart->get_fee_total();
          
            $shipping_total = $cart->get_shipping_total();
         
            $cart->set_cart_contents_taxes($product_taxes);
            $cart->set_total_tax($taxes);
            $total_tax = $cart->get_total_tax();      
            $discount = $cart->get_discount_total();
            $discount_tax = $cart->get_discount_tax();
            $total_discount = $discount + $discount_tax;
            $cart->set_subtotal($subtotal);
            $cart->set_total($subtotal + $total_tax + $shipping_total + $fees - $total_discount);      
        }
    }
    add_action('woocommerce_after_calculate_totals', 'crew_b2b_cart_totals_rounding');

    Hi @ageorgiev

    Thanks for reaching out!

    I’m glad you were able to find a solution to your inquiry here and thanks for sharing it with the community too! ??

    I will be marking this thread as resolved. Should you have further inquiries, kindly create a new topic here.

    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Calculate and round prices to 2 digits’ is closed to new replies.