-
Notifications
You must be signed in to change notification settings - Fork 10.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use proper location for taxes when adding products via admin #30692
Conversation
The order is optional and must be passed as an "order" key in "$args". If present, the order customer will be passed to "WC_Tax::get_rates" in order to calculate the taxes to substract (provided that "$product->is_taxable() && wc_prices_include_tax()") Also "WC_Abstract_Order->add_product" and "WC_AJAX::maybe_add_order_item" are modified to pass the order being processed so that ultimately "wc_get_price_excluding_tax" uses it. This prevents the base tax location to be wrongly used instead of the customer location when the "woocommerce_adjust_non_base_location_prices" hook is set to return false.
The test verifies that when an order is passed as an argument, the order customer is passed to 'WC_Tax::get_rates'.
23bab7f
to
98a2391
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Given that this is a very fundamental change, what do you think about adding more tests? I think we can add at least for:
add_product
method when order is passed in $args with customer having different base address.add_product
method when order is passed in $args with customer having similar base address.add_product
method when order is passed in $args without customer.- ``add_product` method without order.
Maybe some more variations for when fileter is disabled/enabled as well?
@vedanshujain I think that since we are already testing that |
The test verifies that if an order is passed in arguments, that order is passed in turn to wc_get_price_excluding_tax.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Hi @vedanshujain, thanks for merging this pull request. Please take a look at these follow-up tasks you may need to perform:
|
PR #30692 modified 'wc_get_price_excluding_tax' so that if an order is passed its customer will be passed to WC_Tax::get_rates in order to use the proper location for the taxes to be discounted. The problem is that when the order has no customer (it's "Guest") an invalid customer (id=0) is passed, which has no location, and thus no taxes are deducted whatsoever. The fix consists of checking if the customer id from the order is 0, and in that case no customer is passed to WC_Tax::get_rates, thus the shop location is used for the taxes.
PR #30692 modified 'wc_get_price_excluding_tax' so that if an order is passed its customer will be passed to WC_Tax::get_rates in order to use the proper location for the taxes to be discounted. The problem is that when the order has no customer (it's "Guest") an invalid customer (id=0) is passed, which has no location, and thus no taxes are deducted whatsoever. The fix consists of checking if the customer id from the order is 0, and in that case no customer is passed to WC_Tax::get_rates, thus the shop location is used for the taxes.
All Submissions:
Changes proposed in this Pull Request:
When the
woocommerce_adjust_non_base_location_prices
is set to always use the customer location to calculate taxes, and "I will enter prices inclusive of tax" setting is set, adding a product to an order as an admin via the order editor will calculate the wrong price, because no customer/order data is passed towc_get_price_excluding_tax
and thus the location of the currently logged in user (the shop admin) is used instead of the location of the customer.This PR fixes that by changing
WC_AJAX::maybe_add_order_item
so that it passes the order toWC_Abstract_Order->add_product
, which in turn passes it towc_get_price_excluding_tax
which then obtains the customer from the order and uses his location to calculate the taxes to be substracted from the price.Closes #30231.
How to test the changes in this Pull Request:
add_filter( 'woocommerce_adjust_non_base_location_prices', '__return_false' );
Without the fix, the price that will appear in the line item is 91,67 (110 minus 20% of taxes). With the fix, the price will be 100 (10% of taxes substracted this time).
Other information:
Changelog entry
FOR PR REVIEWER ONLY: