Account for tax classes

This commit is contained in:
Mike Jolley 2015-06-09 14:58:27 +01:00
parent e6ae10ee9a
commit fe58e1b5e9
3 changed files with 105 additions and 83 deletions

View File

@ -865,50 +865,67 @@ class WC_Query {
$min = isset( $_GET['min_price'] ) ? floatval( $_GET['min_price'] ) : 0;
$max = isset( $_GET['max_price'] ) ? floatval( $_GET['max_price'] ) : 9999999999;
$tax_display_mode = get_option( 'woocommerce_tax_display_shop' );
$tax_prices_include_tax = get_option( 'woocommerce_prices_include_tax' );
//check if tax are include and display mode
if($tax_display_mode=='incl' && $tax_prices_include_tax=='no') {
$_tax = new WC_Tax();
$tax_rates= $_tax->get_rates();
foreach($tax_rates as $tax) {
$my_tax = $tax;
break;
}
// If displaying prices in the shop including taxes, but prices don't include taxes..
if ( wc_tax_enabled() && 'incl' === get_option( 'woocommerce_tax_display_shop' ) && ! wc_prices_include_tax() ) {
$tax_classes = array_merge( array( '' ), WC_Tax::get_tax_classes() );
//calculation the negative dif in tax
$tax_down = ( 10000/($my_tax['rate']+100) )/100;
$min =floor($min*$tax_down);
$max =ceil($max*$tax_down);
}
foreach ( $tax_classes as $tax_class ) {
$tax_rates = WC_Tax::get_rates( $tax_class );
$min_class = $min - WC_Tax::get_tax_total( WC_Tax::calc_inclusive_tax( $min, $tax_rates ) );
$max_class = $max - WC_Tax::get_tax_total( WC_Tax::calc_inclusive_tax( $max, $tax_rates ) );
$matched_products_query = apply_filters( 'woocommerce_price_filter_results', $wpdb->get_results( $wpdb->prepare( '
SELECT DISTINCT ID, post_parent, post_type FROM %1$s
INNER JOIN %2$s ON ID = post_id
INNER JOIN %2$s pm1 ON ID = pm1.post_id
INNER JOIN %2$s pm2 ON ID = pm2.post_id
WHERE post_type IN ( "product", "product_variation" )
AND post_status = "publish"
AND meta_key IN ("' . implode( '","', apply_filters( 'woocommerce_price_filter_meta_keys', array( '_price' ) ) ) . '")
AND meta_value BETWEEN %3$d AND %4$d
', $wpdb->posts, $wpdb->postmeta, $min, $max ), OBJECT_K ), $min, $max );
AND pm1.meta_key IN ("' . implode( '","', apply_filters( 'woocommerce_price_filter_meta_keys', array( '_price' ) ) ) . '")
AND pm1.meta_value BETWEEN %3$d AND %4$d
AND pm2.meta_key = "_tax_class"
AND pm2.meta_value = "%5$s"
', $wpdb->posts, $wpdb->postmeta, $min_class, $max_class, sanitize_title( $tax_class ) ), OBJECT_K ), $min_class, $max_class );
if ( $matched_products_query ) {
foreach ( $matched_products_query as $product ) {
if ( $product->post_type == 'product' ) {
$matched_products[] = $product->ID;
}
if ( $product->post_parent > 0 && ! in_array( $product->post_parent, $matched_products ) ) {
if ( $product->post_parent > 0 ) {
$matched_products[] = $product->post_parent;
}
}
}
}
} else {
$matched_products_query = apply_filters( 'woocommerce_price_filter_results', $wpdb->get_results( $wpdb->prepare( '
SELECT DISTINCT ID, post_parent, post_type FROM %1$s
INNER JOIN %2$s pm1 ON ID = post_id
WHERE post_type IN ( "product", "product_variation" )
AND post_status = "publish"
AND pm1.meta_key IN ("' . implode( '","', apply_filters( 'woocommerce_price_filter_meta_keys', array( '_price' ) ) ) . '")
AND pm1.meta_value BETWEEN %3$d AND %4$d
', $wpdb->posts, $wpdb->postmeta, $min, $max, $tax_class ), OBJECT_K ), $min, $max );
if ( $matched_products_query ) {
foreach ( $matched_products_query as $product ) {
if ( $product->post_type == 'product' ) {
$matched_products[] = $product->ID;
}
if ( $product->post_parent > 0 ) {
$matched_products[] = $product->post_parent;
}
}
}
}
$matched_products = array_unique( $matched_products );
// Filter the id's
if ( 0 === sizeof( $filtered_posts ) ) {
$filtered_posts = $matched_products;
} else {
$filtered_posts = array_intersect( $filtered_posts, $matched_products );
}
$filtered_posts[] = 0;
}

View File

@ -104,7 +104,7 @@ class WC_Tax {
* @param array $rates
* @return array
*/
private static function calc_inclusive_tax( $price, $rates ) {
public static function calc_inclusive_tax( $price, $rates ) {
$taxes = array();
$regular_tax_rates = $compound_tax_rates = 0;
@ -148,7 +148,7 @@ class WC_Tax {
* @param array $rates
* @return array
*/
private static function calc_exclusive_tax( $price, $rates ) {
public static function calc_exclusive_tax( $price, $rates ) {
$taxes = array();
if ( $rates ) {

View File

@ -161,15 +161,20 @@ class WC_Widget_Price_Filter extends WC_Widget {
$form_action = preg_replace( '%\/page/[0-9]+%', '', home_url( trailingslashit( $wp->request ) ) );
}
if ( 'yes' === get_option( 'woocommerce_calc_taxes' ) ) {
if ( 'incl' == get_option( 'woocommerce_tax_display_shop' ) && 'no' == get_option( 'woocommerce_prices_include_tax' ) ) {
$rates = WC_Tax::get_rates();
if ( wc_tax_enabled() && 'incl' === get_option( 'woocommerce_tax_display_shop' ) && ! wc_prices_include_tax() ) {
$tax_classes = array_merge( array( '' ), WC_Tax::get_tax_classes() );
$min = 0;
if ( is_array( $rates ) && ! empty( $rates ) ) {
$_tax = current( $rates );
foreach ( $tax_classes as $tax_class ) {
$tax_rates = WC_Tax::get_rates( $tax_class );
$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 ) );
$min = floor( ( $min ) * ( ( $_tax['rate'] + 100 ) / 100 ) );
$max = ceil( $max * ( ( $_tax['rate'] + 100 ) / 100 ) ) + 1;
if ( $min === 0 || $class_min < $min ) {
$min = $class_min;
}
if ( $class_max > $max ) {
$max = $class_max;
}
}
}