Add getter for tax_display_cart variable, with filter

This commit is contained in:
Mike Jolley 2017-12-15 13:39:35 +00:00
parent fd2c676837
commit 01871fd2c8
7 changed files with 53 additions and 45 deletions

View File

@ -45,11 +45,11 @@ class WC_Cart extends WC_Legacy_Cart {
public $applied_coupons = array(); public $applied_coupons = array();
/** /**
* Are prices in the cart displayed inc or excl tax. * Are prices in the cart displayed inc or excl tax?
* *
* @var string * @var string
*/ */
public $tax_display_cart; public $tax_display_cart = 'incl';
/** /**
* This stores the chosen shipping methods for the cart item packages. * This stores the chosen shipping methods for the cart item packages.
@ -357,6 +357,16 @@ class WC_Cart extends WC_Legacy_Cart {
return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'fee_taxes' ) ); return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'fee_taxes' ) );
} }
/**
* Return whether or not the cart is displaying prices including tax, rather than excluding tax.
*
* @since 3.3.0
* @return bool
*/
protected function display_prices_including_tax() {
return apply_filters( 'woocommerce_cart_' . __FUNCTION__, 'incl' === $this->tax_display_cart );
}
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Setters. | Setters.
@ -951,7 +961,7 @@ class WC_Cart extends WC_Legacy_Cart {
* @return string * @return string
*/ */
public function get_displayed_subtotal() { public function get_displayed_subtotal() {
return 'incl' === $this->tax_display_cart ? $this->get_subtotal() + $this->get_subtotal_tax() : $this->get_subtotal(); return $this->display_prices_including_tax() ? $this->get_subtotal() + $this->get_subtotal_tax() : $this->get_subtotal();
} }
/** /**
@ -1393,21 +1403,21 @@ class WC_Cart extends WC_Legacy_Cart {
public function get_cart_shipping_total() { public function get_cart_shipping_total() {
if ( 0 < $this->get_shipping_total() ) { if ( 0 < $this->get_shipping_total() ) {
if ( 'excl' === $this->tax_display_cart ) { if ( $this->display_prices_including_tax() ) {
$return = wc_price( $this->shipping_total );
if ( $this->shipping_tax_total > 0 && wc_prices_include_tax() ) {
$return .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
return $return;
} else {
$return = wc_price( $this->shipping_total + $this->shipping_tax_total ); $return = wc_price( $this->shipping_total + $this->shipping_tax_total );
if ( $this->shipping_tax_total > 0 && ! wc_prices_include_tax() ) { if ( $this->shipping_tax_total > 0 && ! wc_prices_include_tax() ) {
$return .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>'; $return .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
} }
return $return;
} else {
$return = wc_price( $this->shipping_total );
if ( $this->shipping_tax_total > 0 && wc_prices_include_tax() ) {
$return .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
return $return; return $return;
} }
} else { } else {
@ -1758,18 +1768,18 @@ class WC_Cart extends WC_Legacy_Cart {
if ( $compound ) { if ( $compound ) {
$cart_subtotal = wc_price( $this->get_cart_contents_total() + $this->get_shipping_total() + $this->get_taxes_total( false, false ) ); $cart_subtotal = wc_price( $this->get_cart_contents_total() + $this->get_shipping_total() + $this->get_taxes_total( false, false ) );
} elseif ( 'excl' === $this->tax_display_cart ) { } elseif ( $this->display_prices_including_tax() ) {
$cart_subtotal = wc_price( $this->get_subtotal() );
if ( $this->get_subtotal_tax() > 0 && wc_prices_include_tax() ) {
$cart_subtotal .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
} else {
$cart_subtotal = wc_price( $this->get_subtotal() + $this->get_subtotal_tax() ); $cart_subtotal = wc_price( $this->get_subtotal() + $this->get_subtotal_tax() );
if ( $this->get_subtotal_tax() > 0 && ! wc_prices_include_tax() ) { if ( $this->get_subtotal_tax() > 0 && ! wc_prices_include_tax() ) {
$cart_subtotal .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>'; $cart_subtotal .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
} }
} else {
$cart_subtotal = wc_price( $this->get_subtotal() );
if ( $this->get_subtotal_tax() > 0 && wc_prices_include_tax() ) {
$cart_subtotal .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
} }
return apply_filters( 'woocommerce_cart_subtotal', $cart_subtotal, $compound, $this ); return apply_filters( 'woocommerce_cart_subtotal', $cart_subtotal, $compound, $this );
@ -1782,10 +1792,10 @@ class WC_Cart extends WC_Legacy_Cart {
* @return string formatted price * @return string formatted price
*/ */
public function get_product_price( $product ) { public function get_product_price( $product ) {
if ( 'excl' === $this->tax_display_cart ) { if ( $this->display_prices_including_tax() ) {
$product_price = wc_get_price_excluding_tax( $product );
} else {
$product_price = wc_get_price_including_tax( $product ); $product_price = wc_get_price_including_tax( $product );
} else {
$product_price = wc_get_price_excluding_tax( $product );
} }
return apply_filters( 'woocommerce_cart_product_price', wc_price( $product_price ), $product ); return apply_filters( 'woocommerce_cart_product_price', wc_price( $product_price ), $product );
} }
@ -1806,22 +1816,20 @@ class WC_Cart extends WC_Legacy_Cart {
if ( $product->is_taxable() ) { if ( $product->is_taxable() ) {
if ( 'excl' === $this->tax_display_cart ) { if ( $this->display_prices_including_tax() ) {
$row_price = wc_get_price_excluding_tax( $product, array( 'qty' => $quantity ) );
$product_subtotal = wc_price( $row_price );
if ( wc_prices_include_tax() && $this->get_subtotal_tax() > 0 ) {
$product_subtotal .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
} else {
$row_price = wc_get_price_including_tax( $product, array( 'qty' => $quantity ) ); $row_price = wc_get_price_including_tax( $product, array( 'qty' => $quantity ) );
$product_subtotal = wc_price( $row_price ); $product_subtotal = wc_price( $row_price );
if ( ! wc_prices_include_tax() && $this->get_subtotal_tax() > 0 ) { if ( ! wc_prices_include_tax() && $this->get_subtotal_tax() > 0 ) {
$product_subtotal .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>'; $product_subtotal .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
} }
} else {
$row_price = wc_get_price_excluding_tax( $product, array( 'qty' => $quantity ) );
$product_subtotal = wc_price( $row_price );
if ( wc_prices_include_tax() && $this->get_subtotal_tax() > 0 ) {
$product_subtotal .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
} }
} else { } else {
$row_price = $price * $quantity; $row_price = $price * $quantity;

View File

@ -115,7 +115,7 @@ abstract class WC_Legacy_Cart {
break; break;
case 'display_totals_ex_tax' : case 'display_totals_ex_tax' :
case 'display_cart_ex_tax' : case 'display_cart_ex_tax' :
$value = 'excl' === $this->tax_display_cart; $value = ! $this->display_prices_including_tax();
break; break;
case 'cart_contents_weight' : case 'cart_contents_weight' :
$value = $this->get_cart_contents_weight(); $value = $this->get_cart_contents_weight();

View File

@ -162,7 +162,7 @@ class WC_Shipping_Free_Shipping extends WC_Shipping_Method {
if ( in_array( $this->requires, array( 'min_amount', 'either', 'both' ) ) ) { if ( in_array( $this->requires, array( 'min_amount', 'either', 'both' ) ) ) {
$total = WC()->cart->get_displayed_subtotal(); $total = WC()->cart->get_displayed_subtotal();
if ( 'incl' === WC()->cart->tax_display_cart ) { if ( WC()->cart->display_prices_including_tax() ) {
$total = round( $total - ( WC()->cart->get_discount_total() + WC()->cart->get_discount_tax() ), wc_get_price_decimals() ); $total = round( $total - ( WC()->cart->get_discount_total() + WC()->cart->get_discount_tax() ), wc_get_price_decimals() );
} else { } else {
$total = round( $total - WC()->cart->get_discount_total(), wc_get_price_decimals() ); $total = round( $total - WC()->cart->get_discount_total(), wc_get_price_decimals() );

View File

@ -176,7 +176,7 @@ class WC_Shipping_Legacy_Free_Shipping extends WC_Shipping_Method {
if ( in_array( $this->requires, array( 'min_amount', 'either', 'both' ) ) ) { if ( in_array( $this->requires, array( 'min_amount', 'either', 'both' ) ) ) {
$total = WC()->cart->get_displayed_subtotal(); $total = WC()->cart->get_displayed_subtotal();
if ( 'incl' === WC()->cart->tax_display_cart ) { if ( WC()->cart->display_prices_including_tax() ) {
$total = round( $total - ( WC()->cart->get_discount_total() + WC()->cart->get_discount_tax() ), wc_get_price_decimals() ); $total = round( $total - ( WC()->cart->get_discount_total() + WC()->cart->get_discount_tax() ), wc_get_price_decimals() );
} else { } else {
$total = round( $total - WC()->cart->get_discount_total(), wc_get_price_decimals() ); $total = round( $total - WC()->cart->get_discount_total(), wc_get_price_decimals() );

View File

@ -299,7 +299,7 @@ function wc_cart_totals_order_total_html() {
$value = '<strong>' . WC()->cart->get_total() . '</strong> '; $value = '<strong>' . WC()->cart->get_total() . '</strong> ';
// If prices are tax inclusive, show taxes here. // If prices are tax inclusive, show taxes here.
if ( wc_tax_enabled() && WC()->cart->tax_display_cart == 'incl' ) { if ( wc_tax_enabled() && WC()->cart->display_prices_including_tax() ) {
$tax_string_array = array(); $tax_string_array = array();
$cart_tax_totals = WC()->cart->get_tax_totals(); $cart_tax_totals = WC()->cart->get_tax_totals();
@ -329,7 +329,7 @@ function wc_cart_totals_order_total_html() {
* @param object $fee * @param object $fee
*/ */
function wc_cart_totals_fee_html( $fee ) { function wc_cart_totals_fee_html( $fee ) {
$cart_totals_fee_html = ( 'excl' == WC()->cart->tax_display_cart ) ? wc_price( $fee->total ) : wc_price( $fee->total + $fee->tax ); $cart_totals_fee_html = WC()->cart->display_prices_including_tax() ? wc_price( $fee->total + $fee->tax ) : wc_price( $fee->total );
echo apply_filters( 'woocommerce_cart_totals_fee_html', $cart_totals_fee_html, $fee ); echo apply_filters( 'woocommerce_cart_totals_fee_html', $cart_totals_fee_html, $fee );
} }
@ -343,16 +343,16 @@ function wc_cart_totals_shipping_method_label( $method ) {
$label = $method->get_label(); $label = $method->get_label();
if ( $method->cost > 0 ) { if ( $method->cost > 0 ) {
if ( WC()->cart->tax_display_cart == 'excl' ) { if ( WC()->cart->display_prices_including_tax() ) {
$label .= ': ' . wc_price( $method->cost );
if ( $method->get_shipping_tax() > 0 && wc_prices_include_tax() ) {
$label .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
} else {
$label .= ': ' . wc_price( $method->cost + $method->get_shipping_tax() ); $label .= ': ' . wc_price( $method->cost + $method->get_shipping_tax() );
if ( $method->get_shipping_tax() > 0 && ! wc_prices_include_tax() ) { if ( $method->get_shipping_tax() > 0 && ! wc_prices_include_tax() ) {
$label .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>'; $label .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
} }
} else {
$label .= ': ' . wc_price( $method->cost );
if ( $method->get_shipping_tax() > 0 && wc_prices_include_tax() ) {
$label .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
} }
} }

View File

@ -65,7 +65,7 @@ if ( ! defined( 'ABSPATH' ) ) {
</tr> </tr>
<?php endforeach; ?> <?php endforeach; ?>
<?php if ( wc_tax_enabled() && 'excl' === WC()->cart->tax_display_cart ) : <?php if ( wc_tax_enabled() && ! WC()->cart->display_prices_including_tax() ) :
$taxable_address = WC()->customer->get_taxable_address(); $taxable_address = WC()->customer->get_taxable_address();
$estimated_text = WC()->customer->is_customer_outside_base() && ! WC()->customer->has_calculated_shipping() $estimated_text = WC()->customer->is_customer_outside_base() && ! WC()->customer->has_calculated_shipping()
? sprintf( ' <small>' . __( '(estimated for %s)', 'woocommerce' ) . '</small>', WC()->countries->estimated_for_prefix( $taxable_address[0] ) . WC()->countries->countries[ $taxable_address[0] ] ) ? sprintf( ' <small>' . __( '(estimated for %s)', 'woocommerce' ) . '</small>', WC()->countries->estimated_for_prefix( $taxable_address[0] ) . WC()->countries->countries[ $taxable_address[0] ] )

View File

@ -84,7 +84,7 @@ if ( ! defined( 'ABSPATH' ) ) {
</tr> </tr>
<?php endforeach; ?> <?php endforeach; ?>
<?php if ( wc_tax_enabled() && 'excl' === WC()->cart->tax_display_cart ) : ?> <?php if ( wc_tax_enabled() && ! WC()->cart->display_prices_including_tax() ) : ?>
<?php if ( 'itemized' === get_option( 'woocommerce_tax_total_display' ) ) : ?> <?php if ( 'itemized' === get_option( 'woocommerce_tax_total_display' ) ) : ?>
<?php foreach ( WC()->cart->get_tax_totals() as $code => $tax ) : ?> <?php foreach ( WC()->cart->get_tax_totals() as $code => $tax ) : ?>
<tr class="tax-rate tax-rate-<?php echo sanitize_title( $code ); ?>"> <tr class="tax-rate tax-rate-<?php echo sanitize_title( $code ); ?>">