Tax calc for price filter

Co-Authored-By: Gerhard Potgieter <kloon@users.noreply.github.com>
This commit is contained in:
Gerhard 2018-11-16 11:26:49 +02:00 committed by Mike Jolley
parent 3a94fc6d00
commit 31263f2a4c
2 changed files with 47 additions and 14 deletions

View File

@ -795,6 +795,7 @@ function wc_get_product_visibility_options() {
function wc_get_min_max_price_meta_query( $args ) {
$min = isset( $args['min_price'] ) ? floatval( $args['min_price'] ) : 0;
$max = isset( $args['max_price'] ) ? floatval( $args['max_price'] ) : 9999999999;
$src = isset( $args['min_max_source'] ) ? wc_clean( $args['min_max_source'] ) : '';
/**
* Adjust if the store taxes are not displayed how they are stored.
@ -809,8 +810,14 @@ function wc_get_min_max_price_meta_query( $args ) {
$tax_rates = WC_Tax::get_rates( $tax_class );
if ( $tax_rates ) {
$class_min = $min + WC_Tax::get_tax_total( WC_Tax::calc_exclusive_tax( $min, $tax_rates ) );
$class_max = $max - WC_Tax::get_tax_total( WC_Tax::calc_exclusive_tax( $max, $tax_rates ) );
// Price filter widget values already contain taxes, we need to subtract taxes and not add.
if ( 'price_filter_widget' === $src ) {
$class_min = $min - WC_Tax::get_tax_total( WC_Tax::calc_exclusive_tax( $min, $tax_rates ) );
$class_max = $max - WC_Tax::get_tax_total( WC_Tax::calc_exclusive_tax( $max, $tax_rates ) );
} else {
$class_min = $min + WC_Tax::get_tax_total( WC_Tax::calc_exclusive_tax( $min, $tax_rates ) );
$class_max = $max + WC_Tax::get_tax_total( WC_Tax::calc_exclusive_tax( $max, $tax_rates ) );
}
}
}
@ -818,12 +825,16 @@ function wc_get_min_max_price_meta_query( $args ) {
$max = $class_max;
}
return apply_filters( 'woocommerce_get_min_max_price_meta_query', array(
'key' => '_price',
'value' => array( $min, $max ),
'compare' => 'BETWEEN',
'type' => 'DECIMAL(10,' . wc_get_price_decimals() . ')',
), $args );
return apply_filters(
'woocommerce_get_min_max_price_meta_query',
array(
'key' => '_price',
'value' => array( $min, $max ),
'compare' => 'BETWEEN',
'type' => 'DECIMAL(10,' . wc_get_price_decimals() . ')',
),
$args
);
}
/**

View File

@ -80,18 +80,40 @@ class WC_Widget_Price_Filter extends WC_Widget {
wp_enqueue_script( 'wc-price-slider' );
// Find min and max price in current result set.
$prices = $this->get_filtered_price();
$min_price = apply_filters( 'woocommerce_price_filter_widget_min_amount', floor( $prices->min_price / 10 ) * 10 );
$max_price = apply_filters( 'woocommerce_price_filter_widget_max_amount', ceil( $prices->max_price / 10 ) * 10 );
$current_min_price = is_null( $current_min_price ) ? $min_price : floor( $current_min_price / 10 ) * 10;
$current_max_price = is_null( $current_max_price ) ? $max_price : ceil( $current_max_price / 10 ) * 10;
$step = apply_filters( 'woocommerce_price_filter_widget_step', 10 );
$prices = $this->get_filtered_price();
$min_price = $prices->min_price;
$max_price = $prices->max_price;
// Check to see if we should add taxes to the prices if store are excl tax but display incl.
$tax_display_mode = get_option( 'woocommerce_tax_display_shop' );
if ( wc_tax_enabled() && ! wc_prices_include_tax() && 'incl' === $tax_display_mode ) {
$class_min = $min_price;
$class_max = $max_price;
$tax_classes = array_merge( array( '' ), WC_Tax::get_tax_classes() );
foreach ( $tax_classes as $tax_class ) {
$tax_rates = WC_Tax::get_rates( $tax_class );
if ( $tax_rates ) {
$class_min = $min_price + WC_Tax::get_tax_total( WC_Tax::calc_exclusive_tax( $min_price, $tax_rates ) );
$class_max = $max_price + WC_Tax::get_tax_total( WC_Tax::calc_exclusive_tax( $max_price, $tax_rates ) );
}
}
$min_price = $class_min;
$max_price = $class_max;
}
$min_price = apply_filters( 'woocommerce_price_filter_widget_min_amount', floor( $min_price / 10 ) * 10 );
$max_price = apply_filters( 'woocommerce_price_filter_widget_max_amount', ceil( $max_price / 10 ) * 10 );
// If both min and max are equal, we don't need a slider.
if ( $min_price === $max_price ) {
return;
}
$current_min_price = is_null( $current_min_price ) ? $min_price : floor( $current_min_price / 10 ) * 10;
$current_max_price = is_null( $current_max_price ) ? $max_price : ceil( $current_max_price / 10 ) * 10;
$step = apply_filters( 'woocommerce_price_filter_widget_step', 10 );
$this->widget_start( $args, $instance );
if ( '' === get_option( 'permalink_structure' ) ) {