Indexing commerce orders via Drush triggers OrderRefresh

Created on 1 December 2022, almost 2 years ago
Updated 20 May 2023, over 1 year ago

Problem/Motivation

When attempting to (re)index commerce orders via Drush the process is triggering OrderRefresh due to the fact that the majority of orders in the cart are attached to the anonymous user and Drush runs as the anonymous user. This meant that all indexing related tasks had to be performed in the UI.

A secondary issue also meant that the changed timestamp of the cart order was being updated and the cart expiration functionality wouldn't delete these abandoned carts.

Proposed resolution

After investigating the ContentEntity datasource and the CartExpiration QueueWorker in the commerce cart module, I found a commented piece of code:

// Skip the OrderRefresh process to keep the changed timestamp intact.
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
$order = $this->orderStorage->loadUnchanged($order_id);

I have changed the 2 instances of $this->getEntityStorage()->loadMultiple() in the ContentEntity datasource to loadUnchanged() instead.

This now allows for the commerce orders to be successfully indexed via Drush which is much quicker and OrderRefresh is no longer incorrectly fired.

Remaining tasks

I will write a patch for my changes, but I'm not 100% convinced that this is the correct approach for all ContentEntity datasources?

πŸ› Bug report
Status

Active

Version

1.0

Component

General code

Created by

πŸ‡¬πŸ‡§United Kingdom intrafusion Edinburgh, UK

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡¦πŸ‡ΉAustria drunken monkey Vienna, Austria

    Thanks for reporting this problem!

    If Commerce really updates the entities when they are loaded, as it sounds like from the issue summary, then I’d say this is a clear violation of the general contract for CRUD operations. Definitely, we do not want to change our whole code just for this. Especially since I don’t think that indexing orders is a common use case, even for Commerce users.

    One solution to this would be to override the datasource plugin used for entity:commerce_order with a sub-class of \Drupal\search_api\Plugin\search_api\datasource\ContentEntity that uses your proposed adaptions. Maybe the Commerce maintainers would even be prepared to add this as a general addition to the commerce_order module? Please try moving this issue to their issue queue and asking there.

Production build 0.71.5 2024