Order page uses commerce_order_item_table for showing the list of items. This view uses order items as base table, and only use one-way relationship from item to order. This will result in showing orphan items, which will create confusion for users.

The parent issue #3119157: CartManager bug in asynchronous multi-threads can be one cause of problem, but there can be other causes.

Comments

skyredwang created an issue. See original summary.

skyredwang’s picture

Status: Active » Needs work
StatusFileSize
new1.28 KB

The patch below adds a required reverse reference relationship, which fixes the display. The relationship change the query from

SELECT commerce_order_item.order_item_id AS order_item_id
FROM
{commerce_order_item} commerce_order_item
WHERE (commerce_order_item.order_id = '12345')

to

SELECT commerce_order_item.order_item_id AS order_item_id, commerce_order_commerce_order_item.order_id AS commerce_order_commerce_order_item_order_id, commerce_order_item_commerce_order.order_item_id AS commerce_order_item_commerce_order_order_item_id
FROM
{commerce_order_item} commerce_order_item
INNER JOIN {commerce_order} commerce_order_commerce_order_item ON commerce_order_item.order_id = commerce_order_commerce_order_item.order_id
INNER JOIN {commerce_order_item} commerce_order_item_commerce_order ON commerce_order_commerce_order_item.order_id = commerce_order_item_commerce_order.order_id
WHERE (commerce_order_item.order_id = '12345')

But, this relationship comes from `cart` module; I don't like adding the `cart` dependency to `order`

agoradesign’s picture

josh.stewart’s picture

@skyredwang - I 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 patch to the view didn't solve my issue.

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!

luksak’s picture

I adapted my view with the relationships of the patch in #2, but that didn't get rid of the orphan items for me. It rather added two duplicates of every existing line item.

luksak’s picture

I found a temporary workaround that fixes the issue for me:

<?php
/*
 * Implements hook_views_pre_render().
 */
function MODULE_views_pre_render(ViewExecutable $view) {
  if ($view->id() === 'commerce_order_item_table') {
    foreach ($view->result as $key => $item) {
      /** @var \Drupal\commerce_order\Entity\OrderItem $order_item */
      $order_item = $item->_entity;
      $order = $order_item->getOrder();
      if (!$order->hasItem($order_item)) {
        unset($view->result[$key]);
      }
    }
  }
}
?>
jsacksick’s picture

I think the proper/easiest fix for this would be to update the orders item table view, add an order item ID contextual filter, and pass the order item IDS referenced by the order, instead of an order ID contextual filter.

That prevents the need of adding 2 relationships which causes duplicate results.

The only problem with changing that is that it requires the order item table view to be reverted, and that can potentially mess up people's customizations.

jsacksick’s picture

Status: Needs work » Needs review
StatusFileSize
new2.71 KB

Status: Needs review » Needs work

The last submitted patch, 8: 3119159-8.patch, failed testing. View results

jsacksick’s picture

Status: Needs work » Needs review
StatusFileSize
new2.86 KB

This should hopefully fix the tests.

Status: Needs review » Needs work

The last submitted patch, 10: 3119159-10.patch, failed testing. View results

jsacksick’s picture

Status: Needs work » Needs review
StatusFileSize
new2.89 KB
jsacksick’s picture

I deployed this on a project and for some reason, the view didn't get reverted...

agoradesign’s picture

the revert() command skips modified configuration by default - so if your project had a modified commerce_order_item_table view, it won't get changed

jsacksick’s picture

Issue tags: +Needs change record

  • jsacksick committed 3389831 on 8.x-2.x
    Issue #3119159 by jsacksick, skyredwang: commerce_order_item_table needs...
jsacksick’s picture

Status: Needs review » Fixed

A change record was created. See https://www.drupal.org/node/3197301.

xamount’s picture

Priority: Normal » Critical
Status: Fixed » Needs work

Thanks for the great work...however I am reopening this bug because the committed fix has broken existing Drupal Commerce installations (critically might I add).

After upgrading to the 8.x-2.24, when a user increases the quantity in their shopping cart and then proceeds to the payment page, then the "Order Summary" view displays an inaccurate quantity amount.

I tested this same scenario on Drupal Commerce 8.x-2.21 and it works as expected.

Took me a few hours to track this down.

A quick solution to get it working on Drupal Commerce 8.x-2.24 is to revert the view to use the contextual filter of "Order item: Order" (and not checking "Allow multiple values").

Also there are at least 2 other people in the comments of the change record who are having similar problems. (although I am not sure if all the issues are related).

jsacksick’s picture

Priority: Critical » Normal
Status: Needs work » Fixed

@xamount: What you're describing seems to be a different bug to me...

This isn't touching the order summary view.

Also there are at least 2 other people in the comments of the change record who are having similar problems. (although I am not sure if all the issues are related).

People reported that the view didn't get reverted, and the change records contains instructions for manually updating the contextual filter.

The change introduced in this issue only affects the order items table view.

If you're having a different bug, please open a new issue with clear steps explaining how to reproduce the issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

calbasi’s picture

As commented at #27, order items view has not "Order item: ID" contextual filter by default, but "Order item: order", so your instructions are not accurate. And users, like @xamount or myself could be confused.

I guess in lastest clean installs the contextual filter is "Order item: ID" but I suppose it has changed in some point in the past... Just guessing...

I wonder what "Order item: order" instances should do... Please, re-open this ticket until this important question get solved :-)