The display options for taxes

This commit is contained in:
Mike Jolley 2012-12-03 16:36:54 +00:00
parent 9c74c02578
commit 60626d25f9
10 changed files with 141 additions and 102 deletions

View File

@ -1028,26 +1028,6 @@ $woocommerce_settings['tax'] = apply_filters('woocommerce_tax_settings', array(
),
),
/*array(
'title' => __( 'Tax Display', 'woocommerce' ),
'desc' => __( 'Display cart items excluding tax', 'woocommerce' ),
'id' => 'woocommerce_display_cart_prices_excluding_tax',
'default' => 'yes',
'type' => 'checkbox',
'checkboxgroup' => 'start',
),
array(
'desc' => __( 'Display cart totals excluding tax', 'woocommerce' ),
'id' => 'woocommerce_display_totals_excluding_tax',
'default' => 'yes',
'type' => 'checkbox',
'checkboxgroup' => 'end',
'show_if_checked' => 'yes',
),*/
array( 'type' => 'sectionend', 'id' => 'tax_options' ),
)); // End tax settings

View File

@ -715,12 +715,67 @@ abstract class WC_Product {
/**
* Returns the price (excluding tax) - ignores tax_class filters since the price may *include* tax and thus needs subtracting.
* Returns the price (including tax). Uses customer tax rates. Can work for a specific $qty for more accurate taxes.
*
* @access public
* @return string
*/
function get_price_excluding_tax() {
function get_price_including_tax( $qty = 1 ) {
global $woocommerce;
$_tax = new WC_Tax();
$price = $this->get_price();
if ( $this->is_taxable() ) {
if ( get_option('woocommerce_prices_include_tax') == 'no' ) {
$tax_rates = $_tax->get_rates( $this->get_tax_class() );
$taxes = $_tax->calc_tax( $price * $qty, $tax_rates, false );
$tax_amount = $_tax->get_tax_total( $taxes );
$price = round( $price * $qty + $tax_amount, 2 );
} else {
$tax_rates = $_tax->get_rates( $this->get_tax_class() );
$base_tax_rates = $_tax->get_shop_base_rate( $this->tax_class );
if ( $woocommerce->customer->is_vat_exempt() ) {
$base_taxes = $_tax->calc_tax( $price * $qty, $base_tax_rates, true );
$base_tax_amount = array_sum( $base_taxes );
$price = round( $price * $qty - $base_tax_amount, 2 );
} elseif ( $tax_rates !== $base_tax_rates ) {
$base_taxes = $_tax->calc_tax( $price * $qty, $base_tax_rates, true, true );
$modded_taxes = $_tax->calc_tax( $price * $qty - array_sum( $base_taxes ), $tax_rates, false );
$price = round( $price * $qty - array_sum( $base_taxes ) + array_sum( $modded_taxes ), 2 );
} else {
$price = $price * $qty;
}
}
} else {
$price = $price * $qty;
}
return apply_filters( 'woocommerce_get_price_including_tax', $price, $qty, $this );
}
/**
* Returns the price (excluding tax) - ignores tax_class filters since the price may *include* tax and thus needs subtracting.
* Uses store base tax rates. Can work for a specific $qty for more accurate taxes.
*
* @access public
* @return string
*/
function get_price_excluding_tax( $qty = 1 ) {
$price = $this->get_price();
@ -728,12 +783,15 @@ abstract class WC_Product {
$_tax = new WC_Tax();
$tax_rates = $_tax->get_shop_base_rate( $this->tax_class );
$taxes = $_tax->calc_tax( $price, $tax_rates, true );
$taxes = $_tax->calc_tax( $price * $qty, $tax_rates, true );
$tax_amount = $_tax->get_tax_total( $taxes );
$price = round( $price - $tax_amount, 2);
$price = round( $price * $qty - $tax_amount, 2 );
} else {
$price = $price * $qty;
}
return apply_filters( 'woocommerce_get_price_excluding_tax', $price, $this );
return apply_filters( 'woocommerce_get_price_excluding_tax', $price, $qty, $this );
}

View File

@ -80,10 +80,13 @@ class WC_Cart {
*/
function __construct() {
$this->tax = new WC_Tax();
$this->prices_include_tax = ( get_option('woocommerce_prices_include_tax') == 'yes' ) ? true : false;
$this->display_totals_ex_tax = ( get_option('woocommerce_display_totals_excluding_tax') == 'yes' ) ? true : false;
$this->display_cart_ex_tax = ( get_option('woocommerce_display_cart_prices_excluding_tax') == 'yes' ) ? true : false;
$this->prices_include_tax = ( get_option( 'woocommerce_prices_include_tax' ) == 'yes' ) ? true : false;
$this->tax_display_cart = get_option( 'woocommerce_tax_display_cart' );
$this->dp = ( '' != get_option( 'woocommerce_price_num_decimals' ) ) ? get_option( 'woocommerce_price_num_decimals' ) : 0;
$this->display_totals_ex_tax = $this->tax_display_cart == 'excl' ? true : false;
$this->display_cart_ex_tax = $this->tax_display_cart == 'excl' ? true : false;
add_action( 'init', array( &$this, 'init' ), 5 ); // Get cart on init
}
@ -1580,21 +1583,25 @@ class WC_Cart {
if ( isset( $this->shipping_label ) ) {
if ( $this->shipping_total > 0 ) {
// Display ex tax if the option is set, or prices exclude tax
if ( $this->display_totals_ex_tax || !$this->prices_include_tax ) {
// Display varies depending on settings
if ( $this->tax_display_cart == 'excl' ) {
$return = woocommerce_price( $this->shipping_total );
if ( $this->shipping_tax_total > 0 && $this->prices_include_tax ) {
$return .= ' <small>' . $woocommerce->countries->ex_tax_or_vat() . '</small>';
}
return $return;
} else {
$return = woocommerce_price( $this->shipping_total + $this->shipping_tax_total );
if ( $this->shipping_tax_total > 0 && ! $this->prices_include_tax ) {
$return .= ' <small>' . $woocommerce->countries->inc_tax_or_vat() . '</small>';
}
return $return;
}
@ -1850,8 +1857,8 @@ class WC_Cart {
// Otherwise we show cart items totals only (before discount)
} else {
// Display ex tax if the option is set, or prices exclude tax
if ( $this->display_totals_ex_tax || ! $this->prices_include_tax || $woocommerce->customer->is_vat_exempt() ) {
// Display varies depending on settings
if ( $this->tax_display_cart == 'excl' ) {
$cart_subtotal = woocommerce_price( $this->subtotal_ex_tax );
@ -1866,6 +1873,7 @@ class WC_Cart {
if ( $this->tax_total > 0 && !$this->prices_include_tax ) {
$cart_subtotal .= ' <small>' . $woocommerce->countries->inc_tax_or_vat() . '</small>';
}
}
}
@ -1894,44 +1902,33 @@ class WC_Cart {
// Taxable
if ( $taxable ) {
if ( ( $this->display_cart_ex_tax || $woocommerce->customer->is_vat_exempt() ) && $this->prices_include_tax ) {
$base_taxes = $this->tax->calc_tax( $price * $quantity, $base_tax_rates, true );
$base_tax_amount = array_sum( $base_taxes );
$row_price = ( $price * $quantity ) - $base_tax_amount;
if ( $this->tax_display_cart == 'excl' ) {
$row_price = $_product->get_price_excluding_tax( $quantity );
$product_subtotal = woocommerce_price( $row_price );
$product_subtotal .= ' <small class="tax_label">' . $woocommerce->countries->ex_tax_or_vat() . '</small>';
} elseif ( ! $this->display_cart_ex_tax && $tax_rates !== $base_tax_rates && $this->prices_include_tax ) {
$base_taxes = $this->tax->calc_tax( $price * $quantity, $base_tax_rates, true, true );
$modded_taxes = $this->tax->calc_tax( ( $price * $quantity ) - array_sum( $base_taxes ), $tax_rates, false );
$row_price = (( $price * $quantity ) - array_sum( $base_taxes )) + array_sum( $modded_taxes );
$product_subtotal = woocommerce_price( $row_price );
if ( ! $this->prices_include_tax ) {
$product_subtotal .= ' <small class="tax_label">' . $woocommerce->countries->inc_tax_or_vat() . '</small>';
}
if ( $this->prices_include_tax )
$product_subtotal .= ' <small class="tax_label">' . $woocommerce->countries->ex_tax_or_vat() . '</small>';
} else {
$row_price = $price * $quantity;
$row_price = $_product->get_price_including_tax( $quantity );
$product_subtotal = woocommerce_price( $row_price );
if ( ! $this->prices_include_tax )
$product_subtotal .= ' <small class="tax_label">' . $woocommerce->countries->inc_tax_or_vat() . '</small>';
}
// Non taxable
// Non-taxable
} else {
$row_price = $price * $quantity;
$row_price = $price * $quantity;
$product_subtotal = woocommerce_price( $row_price );
}
return apply_filters( 'woocommerce_cart_product_subtotal', $product_subtotal, $_product, $quantity, $this );
}
/**

View File

@ -154,9 +154,12 @@ class WC_Order {
* @return void
*/
function __construct( $id = '' ) {
$this->prices_include_tax = (get_option('woocommerce_prices_include_tax')=='yes') ? true : false;
$this->display_totals_ex_tax = (get_option('woocommerce_display_totals_excluding_tax')=='yes') ? true : false;
$this->display_cart_ex_tax = (get_option('woocommerce_display_cart_prices_excluding_tax')=='yes') ? true : false;
$this->prices_include_tax = get_option('woocommerce_prices_include_tax') == 'yes' ? true : false;
$this->tax_display_cart = get_option( 'woocommerce_tax_display_cart' );
$this->display_totals_ex_tax = $this->tax_display_cart == 'excl' ? true : false;
$this->display_cart_ex_tax = $this->tax_display_cart == 'excl' ? true : false;
if ( $id > 0 )
$this->get_order( $id );
}
@ -687,12 +690,12 @@ class WC_Order {
if (!isset($item['line_subtotal']) || !isset($item['line_subtotal_tax'])) return;
if ( $this->display_cart_ex_tax || ! $this->prices_include_tax ) :
if ( $this->tax_display_cart == 'excl' ) {
if ( $this->prices_include_tax ) $ex_tax_label = 1; else $ex_tax_label = 0;
$subtotal = woocommerce_price( $this->get_line_subtotal( $item ), array('ex_tax_label' => $ex_tax_label ) );
else :
$subtotal = woocommerce_price( $this->get_line_subtotal( $item ), array( 'ex_tax_label' => $ex_tax_label ) );
} else {
$subtotal = woocommerce_price( $this->get_line_subtotal( $item, true ) );
endif;
}
return apply_filters( 'woocommerce_order_formatted_line_subtotal', $subtotal, $item, $this );
}
@ -724,54 +727,54 @@ class WC_Order {
$subtotal = 0;
if ( ! $compound ) :
if ( ! $compound ) {
foreach ($this->get_items() as $item) :
foreach ( $this->get_items() as $item ) {
if ( ! isset( $item['line_subtotal'] ) || ! isset( $item['line_subtotal_tax'] ) ) return;
$subtotal += $this->get_line_subtotal( $item );
if ( ! $this->display_cart_ex_tax ) :
if ( $this->tax_display_cart == 'incl' ) {
$subtotal += $item['line_subtotal_tax'];
endif;
}
endforeach;
}
$subtotal = woocommerce_price( $subtotal );
if ( $this->display_cart_ex_tax && $this->prices_include_tax ) :
if ( $this->tax_display_cart == 'excl' && $this->prices_include_tax )
$subtotal .= ' <small>'.$woocommerce->countries->ex_tax_or_vat().'</small>';
endif;
else :
} else {
if ( $this->prices_include_tax ) return;
if ( $this->tax_display_cart == 'incl' )
return;
foreach ($this->get_items() as $item) :
foreach ( $this->get_items() as $item ) {
$subtotal += $item['line_subtotal'];
endforeach;
}
// Add Shipping Costs
$subtotal += $this->get_shipping();
// Remove non-compound taxes
foreach ( $this->get_taxes() as $tax ) :
foreach ( $this->get_taxes() as $tax ) {
if ( ! empty( $tax['compound'] ) ) continue;
$subtotal = $subtotal + $tax['tax_amount'] + $tax['shipping_tax_amount'];
endforeach;
}
// Remove discounts
$subtotal = $subtotal - $this->get_cart_discount();
$subtotal = woocommerce_price( $subtotal );
endif;
}
return apply_filters( 'woocommerce_order_subtotal_to_display', $subtotal, $compound, $this );
}
@ -786,35 +789,35 @@ class WC_Order {
function get_shipping_to_display() {
global $woocommerce;
if ( $this->order_shipping > 0 ) :
if ( $this->order_shipping > 0 ) {
$tax_text = '';
if ($this->display_totals_ex_tax || !$this->prices_include_tax) :
if ( $this->tax_display_cart == 'excl' ) {
// Show shipping excluding tax
$shipping = woocommerce_price($this->order_shipping);
if ($this->order_shipping_tax > 0 && $this->prices_include_tax) :
$tax_text = $woocommerce->countries->ex_tax_or_vat() . ' ';
endif;
$shipping = woocommerce_price( $this->order_shipping );
else :
if ( $this->order_shipping_tax > 0 && $this->prices_include_tax )
$tax_text = $woocommerce->countries->ex_tax_or_vat() . ' ';
} else {
// Show shipping including tax
$shipping = woocommerce_price($this->order_shipping + $this->order_shipping_tax);
if ($this->order_shipping_tax > 0 && !$this->prices_include_tax) :
$tax_text = $woocommerce->countries->inc_tax_or_vat() . ' ';
endif;
$shipping = woocommerce_price( $this->order_shipping + $this->order_shipping_tax );
endif;
if ( $this->order_shipping_tax > 0 && ! $this->prices_include_tax )
$tax_text = $woocommerce->countries->inc_tax_or_vat() . ' ';
}
$shipping .= sprintf( __( '&nbsp;<small>%svia %s</small>', 'woocommerce' ), $tax_text, $this->get_shipping_method() );
elseif ( $this->get_shipping_method() ) :
} elseif ( $this->get_shipping_method() ) {
$shipping = $this->get_shipping_method();
else :
} else {
$shipping = __( 'Free!', 'woocommerce' );
endif;
}
return apply_filters( 'woocommerce_order_shipping_to_display', $shipping, $this );
}
@ -889,7 +892,7 @@ class WC_Order {
if ( $fees = $this->get_fees() )
foreach( $fees as $id => $fee ) {
if ( $this->display_cart_ex_tax || ! $this->prices_include_tax ) {
if ( $this->tax_display_cart == 'excl' ) {
$total_rows[ 'fee_' . $id ] = array(
'label' => $fee['name'],
@ -907,7 +910,7 @@ class WC_Order {
}
// Tax for tax exclusive prices
if ( $this->display_cart_ex_tax || ! $this->prices_include_tax ) {
if ( $this->tax_display_cart == 'excl' ) {
if ( sizeof( $this->get_taxes() ) > 0 ) {
$has_compound_tax = false;
@ -963,7 +966,7 @@ class WC_Order {
);
// Tax for inclusive prices
if ( ! $this->display_cart_ex_tax && $this->prices_include_tax ) {
if ( $this->tax_display_cart == 'incl' ) {
$tax_string_array = array();

View File

@ -185,6 +185,7 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc
* Feature - Inline saving of attributes to make creating variable products easier.
* Feature - Zip code restriction for local pickup.
* Feature - New free shipping logic - coupon, min-amount, Both or Either.
* Feature - Taxes can be based on shipping, billing, or shop base.
* Templating - Revised pagination, sorting areas (sorting is now above products, numbered pagination below) and added a result count.
* Templating - email-order-items.php change get_downloadable_file_url() to get_downloadable_file_urls() to support multiple files.

View File

@ -76,7 +76,7 @@ global $woocommerce;
<!-- Product price -->
<td class="product-price">
<?php
$product_price = get_option('woocommerce_display_cart_prices_excluding_tax') == 'yes' || $woocommerce->customer->is_vat_exempt() ? $_product->get_price_excluding_tax() : $_product->get_price();
$product_price = get_option('woocommerce_tax_display_cart') == 'excl' ? $_product->get_price_excluding_tax() : $_product->get_price_including_tax();
echo apply_filters('woocommerce_cart_item_price_html', woocommerce_price( $product_price ), $values, $cart_item_key );
?>

View File

@ -26,7 +26,7 @@ global $woocommerce;
continue;
// Get price
$product_price = get_option( 'woocommerce_display_cart_prices_excluding_tax' ) == 'yes' || $woocommerce->customer->is_vat_exempt() ? $_product->get_price_excluding_tax() : $_product->get_price();
$product_price = get_option( 'woocommerce_tax_display_cart' ) == 'excl' ? $_product->get_price_excluding_tax() : $_product->get_price_including_tax();
$product_price = apply_filters( 'woocommerce_cart_item_price_html', woocommerce_price( $product_price ), $cart_item, $cart_item_key );
?>

View File

@ -17,7 +17,7 @@ if ( $available_methods ) {
$method->full_label = $method->label;
if ( $method->cost > 0 ) {
if ( $woocommerce->cart->display_totals_ex_tax || ! $woocommerce->cart->prices_include_tax || $woocommerce->customer->is_vat_exempt() ) {
if ( $woocommerce->cart->tax_display_cart == 'excl' ) {
$method->full_label .= ': ' . woocommerce_price( $method->cost );
if ( $method->get_shipping_tax() > 0 && $woocommerce->cart->prices_include_tax ) {
$method->full_label .= ' '.$woocommerce->countries->ex_tax_or_vat();

View File

@ -4,7 +4,7 @@
*
* @author WooThemes
* @package WooCommerce/Templates
* @version 1.6.4
* @version 1.7.0
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
@ -52,7 +52,7 @@ $available_methods = $woocommerce->shipping->get_available_shipping_methods();
<tr class="fee fee-<?php echo $fee->id ?>">
<th><?php echo $fee->name ?></th>
<td><?php
if ( $woocommerce->cart->display_totals_ex_tax || ! $woocommerce->cart->prices_include_tax )
if ( $woocommerce->cart->tax_display_cart == 'excl' )
echo woocommerce_price( $fee->amount );
else
echo woocommerce_price( $fee->amount + $fee->tax );
@ -63,7 +63,7 @@ $available_methods = $woocommerce->shipping->get_available_shipping_methods();
<?php
// Show the tax row if showing prices exclusive of tax only
if ( $woocommerce->cart->display_totals_ex_tax || ! $woocommerce->cart->prices_include_tax ) {
if ( $woocommerce->cart->tax_display_cart == 'excl' ) {
$taxes = $woocommerce->cart->get_formatted_taxes();
if ( sizeof( $taxes ) > 0 ) {
@ -125,7 +125,7 @@ $available_methods = $woocommerce->shipping->get_available_shipping_methods();
<strong><?php echo $woocommerce->cart->get_total(); ?></strong>
<?php
// If prices are tax inclusive, show taxes here
if ( ! $woocommerce->cart->display_totals_ex_tax && $woocommerce->cart->prices_include_tax ) {
if ( $woocommerce->cart->tax_display_cart == 'incl' ) {
$tax_string_array = array();
$taxes = $woocommerce->cart->get_formatted_taxes();

View File

@ -57,7 +57,7 @@ $available_methods = $woocommerce->shipping->get_available_shipping_methods();
<tr class="fee fee-<?php echo $fee->id ?>">
<th colspan="2"><?php echo $fee->name ?></th>
<td><?php
if ( $woocommerce->cart->display_totals_ex_tax || ! $woocommerce->cart->prices_include_tax )
if ( $this->tax_display_cart == 'excl' )
echo woocommerce_price( $fee->amount );
else
echo woocommerce_price( $fee->amount + $fee->tax );
@ -68,7 +68,7 @@ $available_methods = $woocommerce->shipping->get_available_shipping_methods();
<?php
// Show the tax row if showing prices exlcusive of tax only
if ( $woocommerce->cart->display_totals_ex_tax || ! $woocommerce->cart->prices_include_tax ) {
if ( $this->tax_display_cart == 'excl' ) {
$taxes = $woocommerce->cart->get_formatted_taxes();
@ -137,7 +137,7 @@ $available_methods = $woocommerce->shipping->get_available_shipping_methods();
<strong><?php echo $woocommerce->cart->get_total(); ?></strong>
<?php
// If prices are tax inclusive, show taxes here
if ( ! $woocommerce->cart->display_totals_ex_tax && $woocommerce->cart->prices_include_tax ) {
if ( $this->tax_display_cart == 'incl' ) {
$tax_string_array = array();
$taxes = $woocommerce->cart->get_formatted_taxes();