Skip to content

fix(woocommerce): subtract tax from price filter bounds#4338

Open
faisalahammad wants to merge 1 commit into
10up:developfrom
faisalahammad:fix/4332-woo-price-filter-tax
Open

fix(woocommerce): subtract tax from price filter bounds#4338
faisalahammad wants to merge 1 commit into
10up:developfrom
faisalahammad:fix/4332-woo-price-filter-tax

Conversation

@faisalahammad

Copy link
Copy Markdown

Summary

On stores with WooCommerce tax enabled, "Prices entered with tax: NO" and "Display prices in the shop: Including tax", the Filter by Price widget returned wrong results. ElasticPress indexed the excluding-tax _price but compared the user's including-tax min_price/max_price bounds directly against it, with no tax conversion on either side. Disabling ElasticPress fixed it because WooCommerce core's native filter converts the bounds.

This fix mirrors WooCommerce core (WC_Query::price_filter_post_clauses): when the shop displays including-tax prices but prices are entered excluding tax, the inclusive tax is subtracted from the bounds before the Elasticsearch range query so they line up with the excluding-tax indexed price.

Fixes #4332

Changes

includes/classes/Feature/WooCommerce/Products.php

Before:

$min_price = ! empty( $_GET['min_price'] ) ? sanitize_text_field( wp_unslash( $_GET['min_price'] ) ) : null;
$max_price = ! empty( $_GET['max_price'] ) ? sanitize_text_field( wp_unslash( $_GET['max_price'] ) ) : null;
// phpcs:enable WordPress.Security.NonceVerification

if ( $query->is_search() ) {

After:

$min_price = ! empty( $_GET['min_price'] ) ? sanitize_text_field( wp_unslash( $_GET['min_price'] ) ) : null;
$max_price = ! empty( $_GET['max_price'] ) ? sanitize_text_field( wp_unslash( $_GET['max_price'] ) ) : null;
// phpcs:enable WordPress.Security.NonceVerification

// Align bounds with the excluding-tax price Elasticsearch indexes when the
// shop shows including-tax prices, matching WooCommerce core.
if ( null !== $min_price ) {
    $min_price = $this->get_price_filter_tax_adjustment( (float) $min_price );
}
if ( null !== $max_price ) {
    $max_price = $this->get_price_filter_tax_adjustment( (float) $max_price );
}

if ( $query->is_search() ) {

Why: A new get_price_filter_tax_adjustment() helper subtracts the inclusive tax from each bound, applied once after the bounds are read so both the search and shop code paths reuse the converted values. No-op whenever tax is off, no tax rates are configured, prices are entered including tax, or the shop displays excluding tax, so unaffected stores behave exactly as before. No mapping or index change, so no reindex is needed.

Testing

Test 1: Reproduce the bug fix (unit)

  1. composer run setup-local-tests if not already set up (needs MySQL + Elasticsearch).
  2. vendor/bin/phpunit --filter testPriceFilterWithTax tests/php/features/WooCommerce/TestWooCommerceProduct.php
  3. The test seeds a 20% tax rate, sends min_price=120&max_price=120, and asserts the Elasticsearch range bound is reduced to 100.0 (the excluding-tax price).
    Result: passes.

Test 2: Existing price filters still green

  1. vendor/bin/phpunit --filter testPriceFilter tests/php/features/WooCommerce/TestWooCommerceProduct.php
  2. vendor/bin/phpunit --filter testPriceFilterWithSearchQuery tests/php/features/WooCommerce/TestWooCommerceProduct.php
    Result: both pass (tax adjustment is a no-op when tax is off).

Test 3: Manual repro (issue #4332)

  1. WooCommerce > Settings > Tax: enable tax, prices entered excluding tax, display including tax.
  2. Add a 20% standard rate for the base location.
  3. Create a product priced 100 (excl tax); it shows 120 (incl tax).
  4. Filter ?min_price=120&max_price=120.
    Result: product appears (before fix it did not).

The Filter by Price widget compared including-tax bounds against the
excluding-tax price indexed in Elasticsearch, so products were dropped.
Mirror WooCommerce core by subtracting inclusive tax from min/max bounds
when prices are entered excluding tax but the shop shows including tax.

Fixes 10up#4332
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Woo showing wrong results when filtering by price and tax is enabled

1 participant