I have noticed that I have many orphans of commerce_order_items in my table which have not an entry in commerce_order__order_items table.
When I run the following SQL query:
SELECT
c.order_id,
c.purchased_entity,
c.title,
c.created,
co.order_items_target_id
FROM
commerce_order_item c
JOIN
commerce_order_item ci ON ci.order_id = c.order_id AND ci.purchased_entity = c.purchased_entity AND c.uuid != ci.uuid
LEFT JOIN
commerce_order__order_items co ON c.order_item_id = co.order_items_target_id
ORDER BY c.order_id DESC
LIMIT 0,20;
I can see as a result things like:
ORDER ID ID TITLE CREATED REF to commerce_order__order_items
11387563 1183 Product 1 1563791135 4731849
11387563 1183 Product 1 1563791135 NULL
11387563 1331 Product 2 1563791083 NULL
11387563 1331 Product 2 1563791083 4731846
Last column shouldn't be NULL and you can also see that created has the same timestamp.
In terms of calculating the correct total price everything is just fine. But these orphans are also visible on the receipt for example.
This behavior happens every 20 orders....
The problem is I am NOT able to reproduce this behavior on my local env or even on production. BUT I can see in the DB that this is happening.
Btw..Combine is active in the CartManager in addOrderItem method.
Any thoughts on this one?
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | order_detail_view.png | 68.23 KB | carsteng |
Comments
Comment #2
bojanz commentedCould it be caused by modifying the order in the admin UI somehow? Otherwise, maybe some custom code (a cron job?) that only kicks in occasionally?
Comment #3
bojanz commentedClosing this issue for now, since we cannot reproduce it.
I do wonder if it was caused by a buggy admin form. Commerce 2.14 fixed some bugs where inline forms were auto-saved when another element was triggered, I wonder if that was the cause.
Comment #4
yseki commentedHi @bojanz,
I'm having the same issue as @carstenG described.
I can't reproduce it intentionally, but what I've discovered about this is that the Commerce module has two ways to reference items into orders: There is this "foreign key" in
commerce_order_itemtable, that is used on the view witch presents the order itself:/admin/commerce/orders/ORDER_IDAnd there is this other relation tablecommerce_order__order_items, that's used to load the order edit page/admin/commerce/orders/ORDER_ID/edit.The inconsistency is between the order view page and the order edit page.
As I needed a quick fix for that on the project I'm working, I removed the inconsistent data from
commerce_order_itemtable. It was necessary mainly because with this inconsistency, the Total on the view page seems to be wrongly calculated, but in fact it's the same as the value payed by the client.This issue doesn't seem to be harmful as the correct values and logic are working perfectly, but it's causing misunderstood for the administrators (they keep complaining about the issue) .
Thanks a lot for this module!
Comment #5
carsteng commentedComing back to this one here. The issue still remains and is definitely not caused by "buggy" admin forms.
As you can see from the image this happens through the "add to cart" operation.
There is dc_ajax_add_cart in place.
Comment #6
bojanz commentedUnfortunately, I still have no way to reproduce, so a fix from me is not possible.
Comment #7
vood002 commentedI'm also experiencing this issue. I also tried to reproduce manually and was unable to do so, but I have my first order in the system that is displaying the exact scenario referenced here, namely an item exists in the commerce_order_item table which I see from the log was added to, then removed from the cart. This item does not exist in the commerce_order__order_items table.
I'm not sure how to go about debugging this one either. I can't find any documentation about why these two tables exist which seem to serve the same purpose.
Comment #8
introfini commentedHi,
Same issue here. I can't reproduce it either but also happens every twenty orders or so. The timestamps aren't always the same.
The 2 major problems with this issue are:
Comment #9
introfini commentedFor others having the same issue until this get sorted out.
Every time the cron runs it removes those orphan rows:
Comment #10
agoradesign commentedadded #3119159: commerce_order_item_table needs to filter out orphan order items as a related issue
Comment #11
papai_8 commentedI was having the same issue of line items being orphaned when I delete using $order->removeItem(). The only way to fix this was to use the delete method on the item itself.
Comment #12
introfini commentedI'm also having this issue when the user removes order items from the cart. It only happens sometimes, very rarely, like 1 in 50 orders.
Comment #13
josh.stewart commentedI ran into this issue today. Order came through and the commerce_order_item_table had 30+ items in the table, then the edit screen only had the one item actually ordered.
I might be able to provide some context of how it came about, too, in case that helps figure out the issue. Adding that cron hook to my custom module didn't solve my issue either, unfortunately. Was wondering if it was supposed to fix all the previously problematic views displays.
I was doing some random testing on adding items to carts as anonymous users very quickly. At one point there were 3 or 4 anonymous carts open. Then, I went in and just manually deleted the carts through /admin/commerce/orders/carts by bulk deleting them when I was finished testing a few things. I haven't confirmed this was the actual cause of the issue but what is showing up in the commerce_order_item_table looks suspiciously like the items I was randomly adding to carts and the order that was displaying weird in commerce_order_item_table was the very next order processed.
Hopefully helps to get to the bottom of this. Cannot figure out how to fix the table for my client.
Thanks!
Comment #14
elex commentedI also have this issue but cannot reproduce. It happened during the removal of order items. Not all were removed from table commerce_order_item.
Comment #15
luksakI also have this issue. I think in some cases commerce_combine_carts is causing this. It is merging the carts and creates items that the customer never pays for, but show up in the backend.
Recently I discovered orders that didn't combine also have this issue.
I think those are two separate issues, event if the resulting problems are similar. When running the query in #9, the orders that were not combined, get fixed. The others stay the way they are.
Reproducing this issue is really hard. I also have a very low percentage of orders that have this issue.
Comment #16
luksakBumping priority, since my client currently ships items for which the customers didn't pay for :)
I'll be looking into a quick fix in #3119159: commerce_order_item_table needs to filter out orphan order items for the contrib weekend.
Comment #17
luksakI found a workaround an posted it here: https://www.drupal.org/project/commerce/issues/3119159#comment-13981362
Comment #18
star-szrThe related issue #3119159: commerce_order_item_table needs to filter out orphan order items has been resolved, can we close this one? Does updating commerce and updating your views config as noted in https://www.drupal.org/node/3197301 solve this for you?
Comment #19
harcher commentedHaving the same issue with random orphaned products in the 'commerce_order_item' table.
The query in #9 did not remove these orphans.
Surely a simpler query in one that selects the rows from the 'commerce_order_item' table that are not part of any order in the 'commerce_order' table. Just wondering why @introfini didn't do that instead. Here's the query:
$database->query("DELETE {commerce_order_item} FROM {commerce_order_item} LEFT JOIN {commerce_order} ON {commerce_order_item}.order_id = {commerce_order}.order_id WHERE {commerce_order}.order_id IS NULL;");Is it ok to delete these orphans like this or is it possible these rows are still needed even though they don't belong to any order?
Comment #20
star-szrI may have misread this issue, un-postponing for now. Seems like the fix to the commerce_order_item_table "component" should fix this for some, but perhaps not for everyone.
Comment #21
abx commentedI'm not sure if it's the same problem in Commerce Ajax ATC (Add to cart) ( https://www.drupal.org/project/commerce_ajax_atc/issues/3471969 ), since I got similar problem with that module.
I also just tried Ajax Add to Cart module ( https://www.drupal.org/project/ajax_add_to_cart ). Same problem.
If you click "add to cart" quickly enough, you will run into this problem easily for both modules.
Comment #22
abx commentedIn my case, I just found that I have some ECA model that cause this problem. So, it's not problem with Commerce Core or any Ajax cart modules.