From 55b01245cdae067e8a41a9e4272d9401d27e7703 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Fri, 12 Apr 2013 10:59:38 +0100 Subject: [PATCH] Don't round Closes #2861. --- classes/class-wc-cart.php | 77 +++++++++++++++++++++-------------- classes/class-wc-checkout.php | 4 +- readme.txt | 1 + 3 files changed, 49 insertions(+), 33 deletions(-) diff --git a/classes/class-wc-cart.php b/classes/class-wc-cart.php index 540405bbb6d..619f6dd0af6 100644 --- a/classes/class-wc-cart.php +++ b/classes/class-wc-cart.php @@ -1571,55 +1571,71 @@ class WC_Cart { } } - // Set tax total to sum of all tax rows - $this->tax_total = $this->tax->get_tax_total( $this->taxes ); - - // VAT exemption done at this point - so all totals are correct before exemption - if ( $woocommerce->customer->is_vat_exempt() ) { - $this->shipping_tax_total = $this->tax_total = 0; - $this->taxes = $this->shipping_taxes = array(); - - foreach ( $this->cart_contents as $cart_item_key => $item ) - $this->cart_contents[ $cart_item_key ]['line_subtotal_tax'] = $this->cart_contents[ $cart_item_key ]['line_tax'] = 0; - } - - // Cart Discounts (after tax) - $this->apply_cart_discounts_after_tax(); - // Only calculate the grand total + shipping if on the cart/checkout if ( is_checkout() || is_cart() || defined('WOOCOMMERCE_CHECKOUT') || defined('WOOCOMMERCE_CART') ) { + // Total up/round taxes + if ( get_option('woocommerce_tax_round_at_subtotal') == 'no' ) { + $this->tax_total = $this->tax->get_tax_total( $this->taxes ); + $this->taxes = array_map( array( $this->tax, 'round' ), $this->taxes ); + } else { + $this->tax_total = array_sum( $this->taxes ); + } + // Cart Shipping $this->calculate_shipping(); - // VAT exemption for shipping - if ( $woocommerce->customer->is_vat_exempt() ) { - $this->shipping_tax_total = 0; - $this->shipping_taxes = array(); + // Total up/round taxes for shipping + if ( get_option('woocommerce_tax_round_at_subtotal') == 'no' ) { + $this->shipping_tax_total = $this->tax->get_tax_total( $this->shipping_taxes ); + $this->shipping_taxes = array_map( array( $this->tax, 'round' ), $this->shipping_taxes ); + } else { + $this->shipping_tax_total = array_sum( $this->shipping_taxes ); } - // Round cart/shipping tax rows - $this->taxes = array_map( array( $this->tax, 'round' ), $this->taxes ); - $this->shipping_taxes = array_map( array( $this->tax, 'round' ), $this->shipping_taxes ); + // VAT exemption done at this point - so all totals are correct before exemption + if ( $woocommerce->customer->is_vat_exempt() ) + $this->remove_taxes(); + + // Cart Discounts (after tax) + $this->apply_cart_discounts_after_tax(); // Allow plugins to hook and alter totals before final total is calculated do_action( 'woocommerce_calculate_totals', $this ); - /** - * Grand Total - * - * Based on discounted product prices, discounted tax, shipping cost + tax, and any discounts to be added after tax (e.g. store credit) - */ - $this->total = apply_filters( 'woocommerce_calculated_total', number_format( $this->cart_contents_total + $this->tax_total + $this->shipping_tax_total + $this->shipping_total - $this->discount_total + $this->fee_total, $this->dp, '.', '' ), $this ); + // Grand Total - Discounted product prices, discounted tax, shipping cost + tax, and any discounts to be added after tax (e.g. store credit) + $this->total = max( 0, apply_filters( 'woocommerce_calculated_total', number_format( $this->cart_contents_total + $this->tax_total + $this->shipping_tax_total + $this->shipping_total - $this->discount_total + $this->fee_total, $this->dp, '.', '' ), $this ) ); - if ( $this->total < 0 ) - $this->total = 0; + } else { + // Set tax total to sum of all tax rows + $this->tax_total = $this->tax->get_tax_total( $this->taxes ); + + // VAT exemption done at this point - so all totals are correct before exemption + if ( $woocommerce->customer->is_vat_exempt() ) + $this->remove_taxes(); + + // Cart Discounts (after tax) + $this->apply_cart_discounts_after_tax(); } $this->set_session(); } + /** + * remove_taxes function. + * + * @access public + * @return void + */ + public function remove_taxes() { + $this->shipping_tax_total = $this->tax_total = 0; + $this->taxes = $this->shipping_taxes = array(); + + foreach ( $this->cart_contents as $cart_item_key => $item ) + $this->cart_contents[ $cart_item_key ]['line_subtotal_tax'] = $this->cart_contents[ $cart_item_key ]['line_tax'] = 0; + } + /** * looks at the totals to see if payment is actually required. * @@ -1653,7 +1669,6 @@ class WC_Cart { $this->shipping_total = $woocommerce->shipping->shipping_total; // Shipping Total $this->shipping_label = $woocommerce->shipping->shipping_label; // Shipping Label $this->shipping_taxes = $woocommerce->shipping->shipping_taxes; // Shipping Taxes - $this->shipping_tax_total = $this->tax->get_tax_total( $this->shipping_taxes ); // Shipping tax amount } /** diff --git a/classes/class-wc-checkout.php b/classes/class-wc-checkout.php index d701508b589..152b68fcf46 100644 --- a/classes/class-wc-checkout.php +++ b/classes/class-wc-checkout.php @@ -346,8 +346,8 @@ class WC_Checkout { update_post_meta( $order_id, '_order_shipping', woocommerce_format_total( $woocommerce->cart->shipping_total ) ); update_post_meta( $order_id, '_order_discount', woocommerce_format_total( $woocommerce->cart->get_order_discount_total() ) ); update_post_meta( $order_id, '_cart_discount', woocommerce_format_total( $woocommerce->cart->get_cart_discount_total() ) ); - update_post_meta( $order_id, '_order_tax', woocommerce_format_total( $woocommerce->cart->tax_total ) ); - update_post_meta( $order_id, '_order_shipping_tax', woocommerce_format_total( $woocommerce->cart->shipping_tax_total ) ); + update_post_meta( $order_id, '_order_tax', woocommerce_clean( $woocommerce->cart->tax_total ) ); + update_post_meta( $order_id, '_order_shipping_tax', woocommerce_clean( $woocommerce->cart->shipping_tax_total ) ); update_post_meta( $order_id, '_order_total', woocommerce_format_total( $woocommerce->cart->total ) ); update_post_meta( $order_id, '_order_key', apply_filters('woocommerce_generate_order_key', uniqid('order_') ) ); update_post_meta( $order_id, '_customer_user', absint( $this->customer_id ) ); diff --git a/readme.txt b/readme.txt index a3cb297f07c..31ac431b6b7 100644 --- a/readme.txt +++ b/readme.txt @@ -167,6 +167,7 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc = X = * Feature - Option for GA _setDomainName. +* Tweak - Removed rounding when option to round at subtotal is set. * Fix - Allow extra flat rate options even if main rate is 0. * Fix - Fix email subject lines if options not set. * Fix - Prevent over-sanitization of attribute terms when editing products.