Steps to reproduce
1. Create a store with tax setting "Prices are entered with taxes included." unchecked.
2. Create a tax type with "European Union VAT" plugin and "Display taxes of this type inclusive in product prices." checked.
3. Create an order type with "Order refresh frequency" of 1 second. (This pretty much makes the order edit form unsavable so you might want to lower this value only after step 5.)
4. Create an order item type with no "Purchasable entity type", with "Services" "Taxable type" and with Order type from step 3.
5. Create an order of the new type through the admin UI.
After this every time you view the order, the order total increases.
Original report
Hi,
Setup:
The store is set up with prices not being inserted with tax included, the tax region is selected as Estonia.
The tax type being used is EU VAT which is to display the prices with taxes included.
I've created a custom order item type, which has the Purchasable entity type set as "none" and the taxable type is "services"
Issue:
When creating a new order with even just 1 order item of my type, info filled in, and price set to 77.99 (just an example). I made it into a "cart" so I could test checkout, but already then the price had been increased by a multitude of tax increases. Opening up the order and order item, re saving increased it again in the checkout and by now, now even having visited checkout again, it's already somehow 194.06 which is 77.99*1.2*1.2*1.2*1.2*1.2
Update:
It seems that every time the cart/order get's updated the tax get's added again and again, because whenever I add order items to the same order or remove them it's definitely to increase the amount of previous order items by the multiple of 1.2. This behavior is not limited to just Estonian VAT, but also tried with other EU VAT's, the behavior remains.
By now the original item amount has grown to €1,441.87 but tax is €240.31 hence I believe somewhere the total taxed amount is being considered as the amount without tax, and tax get's added to it again, and the same thing happens over and over again. but the tax amount remains correct 20% throughout the whole time :)
| Comment | File | Size | Author |
|---|---|---|---|
| #21 | keep-overriden-unit-price-without-apply-many-times-2907808.patch | 4.75 KB | adelgado12 |
| #19 | keep-overriden-unit-price-2907808.patch | 3.55 KB | adelgado12 |
| #9 | price_increases_on_order_refresh-2907808-9.patch | 3.31 KB | drugan |
Issue fork commerce-2907808
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
drugan commentedI am pretty sure the estonian tax authorities will enjoy this lovely feature of the drupalcommerce :)
Comment #3
Alluuu commentedComment #4
Alluuu commentedComment #5
drugan commentedCan't reproduce your issue.
First, seems you've made some customizations on the code because current latest HEAD does not allow to create variation types other than
commerce_product_variationpurchasable entity type. Yes, you can create order item type of -NONE- purchasable entity type like in your example but you will not see this "option" on the product variation type creation form. Not speaking of the empty string type, i.e.- no purchasable entity type at all.See: https://github.com/drupalcommerce/commerce/blob/8.x-2.x/modules/product/...
However, I've commented out the 81, 82 and 83 lines in the file above and created this variation type and then assigned it to a product type and added to cart the product variation. No warnings, no errors. I've gone through checkout process a couple of times, attempted to resave the order as admin on the order edit page. Nothing unusual, the price €77.99 and the VAT €13.00 stayed unchanged. Then I've gone through example payment process and looked into the order completed. Everything is all right.
Also, I've checked if the €13.00 inclusive VAT is calculated correctly and found that drupalcommerce makes its job flawlessly.
So, more info is needed.
I wonder why you need this kind of -NONE- purchasable entity type because earlier you've already created similar issue:
#2866160: Can not create an order
Comment #6
Alluuu commentedDrugan, in case of order item type without purchasable entity there is no need for product variation as there's no product, just the order item type with it's title, attached fields values and price.
If you wish to recreate the issue create an order item type with no purchasable entity ( the "NONE"), create an order fromadmin commerce->orders (in cart not draft) and add just the custom created order item with title and price. At the first cart visit everything will be correct, but if keep it in cart and don't immediately check out when you add/remove more items to the cart so the cart is refreshed you'll see the increase in price immediately.
My Drupal Commerce install is a fresh unmodified install just installed with composer.
To answer you question why use the purchasable entity NONE order item type: It's a really convenient and nice way to sell per case priced items that you can't or doesn't make sense to make products for. Where the order item is something fully customizable, you create a form to put something together, create an entity and reference to it from the order item and set the calculated price
Comment #7
drugan commentedPlease, notice that current issue is related with order refresh frequency setting (Refresh a draft order when it is loaded regardless of who it belongs to). So, to create/edit an order you need enough time otherwise you'll get error "The content has either been modified by another user, or you have already submitted modifications."
Also, I am not sure if it is expected behaviour but after the first order refresh the VAT component disappears from the price. For example, you've entered €100.00 for an item and saved and went to the order view and see this:
Subtotal €120.00
VAT €20.00
Total €120.00
After refreshing it looks like this:
Subtotal €120.00
Total €120.00
By the way, thanks for your thorough explanation on untyped purchasable entity purpose. I did not know about this just because I don't need that feature in my set up.
Comment #8
bojanz commentedI can confirm this issue. The tax logic modifies the unit price, assuming that it's reset at the beginning of every refresh, which is true for order items backed by purchasable entities, but not true for order items without purchasable entities, and order items with overridden prices.
Comment #9
drugan commentedSorry, somehow forgotten to add use statement for the DrupalDateTime class.
So, ignore the patch on #7.
Comment #10
czigor commentedThe tax logic might have changed since the issue was reported. Now tax does not modify the unit price but it only adds adjustments. And these adjustments are cleared at the beginning of order refresh.
So for me this works as it should, even if the order item has no purchasable entity field.
Comment #11
bojanz commentedThis is probably still an issue. LocalTaxTypeBase modifies the unit price under certain conditions.
Comment #12
czigor commentedAh yes, now I can see it.
Comment #13
czigor commentedAdding steps to reproduce to the issue summary.
Comment #14
czigor commentedI can see 2 ways to proceed:
1. We don't support the "Prices are entered with taxes included." store setting and the "Display taxes of this type inclusive in product prices." tax setting for order items without a purchasable entity or with overridden prices. The hard part here would be making this clear on the UI.
or
2. We add an original_price base field to the order item, which is used on order refresh for order items without a purchasable entity or with overridden prices. We might want to add some methods to OrderItem to get the original price that either gets the variation's price or the order item's original price.
Comment #15
bojanz commentedAdding a related issue.
The underlying problem is that tax promotion submodules will modify the unit price (to add taxes or discounts). That means that the original overridden unit price is going to get lost. We need to store it in a separate field (overriden_unit_price? base_unit_price?) so that we can always restore from that point. We'd also want to drop the overridden_unit_price boolean.
Comment #16
bojanz commentedMarked these issues as duplicate:
- #2965498: Overridden UnitPrice resets by TaxOrderProcessor
- #3044242: Overridden unit price meets order item level promotion
- #2855132: Order Processors causes issue with total calculation when multiple overriding order prices
- #2954000: Overridden Unit Price field gets overridden
Naturally, our fix needs to include reviewing all order processors which modify the unit price.
We are already using "overridden_unit_price" as the name of the boolean, which means we can't use it for the name of the price field.
Furthermore, this field is useful for order items backed by products as well, cause it allows you to refresh an order item after deleting a product (which might be needed when refreshing already placed orders). This means it makes sense to explore a generic name such as base_price or initial_price. The problem with the name "base_price" is that it creates confusion VS unit_price, and I'm not sure if the usage fits the definition.
(EDIT: I see now that my idea above was "base_unit_price" rather than "base_price". Is that any better?)
The second problem is taxes. If your store has tax-inclusive-products, but the customer is tax-free (e.g. lives outside the EU), then a 12eur unit price needs to be reduced to 10eur. If an overridden unit price is entered, do we assume it is the pre-reduction price, or the post-reduction price? I am leaning towards pre-reduction, since that matches the product. However, an argument could be made for vice-versa as well.
Comment #17
au_dave commentedAny updates on this? I am getting the same issue and the patch at #9 no longer applies to the current version of drupal_commerce.
Thanks
David
Comment #18
init90I encounter the issue when adding a discount to the product for which the price can be adjusted by the customer based on the selected options. In the order item we tell that the price is overridden, i.e. 'overridden_unit_price' set to TRUE. This means that on refresh order item price won't be updated according to the product variation price.
With the standard setup, I had 2 issues:
1. It lost the 'overridden_unit_price' setting which lead to the loss customization price.
2. It's updated already updated order item price, which leads to incorrect calculation.
As a workaround, I created a custom promotion plugin that fixes the problems above.
To fix first point the plugin account 'overridden_unit_price' setting:
$order_item->setUnitPrice($new_unit_price, $order_item->isUnitPriceOverridden());To resolve the second point passes the original price to the order item
$order_item->setData('original_price', $price);in add to cart event. Then in the promotion plugin discount always applies to the original price.Hope at some time the issue will be fixed with the solution like in message #16.
Comment #19
adelgado12 commentedAs #18, i have custom price and need to override unit price when product is adding to cart
unfortunately, promotion with display_inclusive lost the overriden unit price boolean field
i found make it works updating promotion class and get overriden unit price field to keep it when apply promotion
Comment #20
jsacksick commentedI don't think the patch from #19 is the correct fix. This will cause the discounted price to be the new overridden unit price, so the price will keep reducing on each refresh.
What we need instead is an
unprocessed_unit_pricefield I guess?But I guess the question is how do we actually restore the overridden unit price on each refresh? I'd say it is the responsability of an order processor that runs early to constantly set the right unit price?
Comment #21
adelgado12 commentedPatch #19, cause regression with promotion applied many times as say in #20
i made another patch to fix and made promotion applied once
Comment #22
tbkot commentedComment #24
tbkot commentedComment #25
anybodyAnother case for this issue: tax-exempt customers with tax-inclusive prices, which bojanz describes in #16. It is the mirror image of the original report: the price shrinks on every refresh instead of growing.
Setup: a German store, prices entered including tax, EU VAT, an order item with an overridden unit price of 11.90 €. The customer is either outside the EU (RS) or an EU B2B customer with a valid VAT number (Intra-Community Supply, #2874167). After three order refreshes:
TaxOrderProcessorremoves the 19% again each time, becauseOrderRefreshdoesn't reset overridden prices.commerce_taxkernel tests pass.So !773 fixes this case too, including the missing override flag in
LocalTaxTypeBase::apply(). It might be worth adding the tax-exempt case to the tests, for example:$order_item->setUnitPrice(new Price('11.90', 'EUR'), TRUE); // Customer outside the EU, store prices include 19% VAT. // Refresh the order several times, the unit price must stay at 10.00 EUR.Problems that already exist without this MR (follow-ups?)
display_inclusive = FALSE): no reduction happens, so the IC customer still pays 11.90 € net.StoreTax::getTaxTypes()only considers display-inclusive tax types, sogetDefaultRates()returns nothing. Exports outside the EU have the same problem. I opened #NEW_ISSUE for this.OrderRefreshdoes not re-resolve overridden prices. This already affects exports, and the MR now extends it to IC orders. #2907808 (MR !773) fixes it: with !773 applied, the price stays at 10.00 €. I commented there.I pushed the
str_ends_with()change and the tests to the MR. Please review.AI disclaimer: This comment was prepared with the help of an AI assistant (Claude Code). I verified the results by running the tests locally.
Comment #26
anybodyComment #27
anybodyComment #28
anybodyWe have tests now, implementation did not change and LGTM, please take a look again.
Also please take a look at the both related issues, I think we should solve them in combination.
Thanks!
Comment #29
anybodyTest-only fails as expected.
Comment #30
jsacksick commented@anybody, @tbkot: Could you review my proposed changes?
I reworked the approach in !773. The logic now lives in
OrderRefreshinstead ofOrderItem::setUnitPrice()and two order processors:OrderRefresh::refresh()already decides which order items get their unit price re-resolved. For the other items (overridden unit price, or no purchased entity), it now resets the unit price to the unprocessed one at that same point, before any order processor runs. Contrib and custom processors that modify the unit price are covered too, whatever their priority.unprocessed_unit_priceandprocessed_unit_price). They are removed when no processor changed the unit price, or when the unit price is re-resolved again, so only order items whose unit price was actually modified store them.OrderItem::setUnitPrice()is unchanged, and the two order processors and the constants are removed.The promotion offers and
LocalTaxTypeBase::apply()now keep the override flag when they set the unit price. Without that, an overridden order item lost its flag and got re-resolved from the variation on the next refresh, losing the custom price (see #18 and #19).The order, promotion and tax kernel tests pass against 3.x, including #3640293 and #2874167.
Comment #31
anybodyThanks @jsacksick! We also have a real-world case for this, I'll also ask @grevil to check it as soon as we can.
At first glance this looks very solid and clean to me!
Should we have further tests or do we have any new risks in this area for things going wrong somewhere now? Do you monitor general test coverage maybe? That might be a helpful indicator for such deep issues.
Comment #32
jsacksick commentedThe entire tests suite is executed with the MR, so at least we know the existing tests aren't failing. I'll check if we can expand the tests coverage a bit more. The good thing about the new logic is we no longer depend on the priority of order processors...
Added
OrderIntegrationTest::testRefreshKeepsChangedUnitPrice(), which makes sure a unit price changed between two refreshes becomes the new unprocessed price instead of being reset to the previous one.Comment #33
jsacksick commented@anybody: I pinged yesterday @tbkot to get second eyes and he agreed with the new approach.
If you can RTBC this, I'll go ahead and merge the MR. It would be great if we could get confirmation the new logic is still working as intented
Comment #34
anybodyThanks @jsacksick that's great news. I don't want to be the blocker here, but I'm very busy on various important issues currently, so can't dig deep enough into this atm. I'll ask @grevil to check this with priority. If someone else here can RTBC it safely before me, please do so!