fix(woocommerce): subtract tax from price filter bounds#4338
Open
faisalahammad wants to merge 1 commit into
Open
fix(woocommerce): subtract tax from price filter bounds#4338faisalahammad wants to merge 1 commit into
faisalahammad wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
_pricebut compared the user's including-taxmin_price/max_pricebounds 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.phpBefore:
After:
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)
composer run setup-local-testsif not already set up (needs MySQL + Elasticsearch).vendor/bin/phpunit --filter testPriceFilterWithTax tests/php/features/WooCommerce/TestWooCommerceProduct.phpmin_price=120&max_price=120, and asserts the Elasticsearch range bound is reduced to100.0(the excluding-tax price).Result: passes.
Test 2: Existing price filters still green
vendor/bin/phpunit --filter testPriceFilter tests/php/features/WooCommerce/TestWooCommerceProduct.phpvendor/bin/phpunit --filter testPriceFilterWithSearchQuery tests/php/features/WooCommerce/TestWooCommerceProduct.phpResult: both pass (tax adjustment is a no-op when tax is off).
Test 3: Manual repro (issue #4332)
?min_price=120&max_price=120.Result: product appears (before fix it did not).