Populate and resolve order item price on CartAddResource

Created on 15 May 2021, almost 4 years ago
Updated 23 February 2023, about 2 years ago

Problem/Motivation

On CartAddResource endpoint on order item create price is not resolved through chain price resolver before order item is added to cart order.
This causes 'CurrencyMismatchException' and 'EntityStorageException' > 'Drupal\Core\Entity\EntityStorageException: Update existing 'commerce_order' entity while changing the ID is not supported. in Drupal\Core\Entity\ContentEntityStorageBase->doPreSave() (line 706 of /var/www/html/web/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php).' on cart order creation if we have multi-currency site and we use Commerce Currency Resolver module for price resolve, e.g. if price is resolved via IP.

Steps to reproduce

Install and configure commerce_currency_resolver module
Switch to not default currency (e.g. if default currency is USD and you use IP-Country for currency resolve > switch IP to some country that uses e.g. EUR currency)
Add item to cart > in CartAddResource->process() order item will be created form PurchasableEntity with unit price that will be resolved as default site currency > on
$order_item = $this->cartManager->addOrderItem($cart, $order_item, $meta['combine'] ?? TRUE); in cartManager cart order will be saved and will trigger Drupal\commerce_currency_resolver\CurrencyOrderProcessor() that will try to resolve price and re save cart order causing 'EntityStorageException' > 'Drupal\Core\Entity\EntityStorageException: Update existing 'commerce_order' entity while changing the ID is not supported. in Drupal\Core\Entity\ContentEntityStorageBase->doPreSave() (line 706 of /var/www/html/web/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php).'

Proposed resolution

As order item unit price is populated and resolved via chainPriceResolver on commerce addToCartForm->buildEntity(), do so on CartAddResource->proccess() to avoid unnecessary order total price resolving on Drupal\commerce_currency_resolver\CurrencyOrderProcessor().

🐛 Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

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.

Production build 0.71.5 2024