AddToCartForm with AJAX re-uses combined order item

Created on 29 February 2024, 10 months ago
Updated 10 May 2024, 8 months ago

Describe your bug or feature request.

If you add AJAX to the AddToCartForm for submit, the internal entity property will always reference the added order item to a cart. If combining order items and the quantity field is hidden, this leads to a doubling of the order item total each time.

This is caused by AddToCartForm::submitForm

    // Ensure we're adding an order_item, not amending one loaded from cache.
    if (!$order_item->isNew()) {
      $order_item = $order_item->createDuplicate();
    }

    $this->entity = $this->cartManager->addOrderItem($cart, $order_item, $form_state->get([
      'settings',
      'combine',
    ]));

The problem is that createDuplicate reuses existing field values and only resets the ID. We need to recreate the order item from the order item storage, like

$this->entity = $this->entityTypeManager->getStorage('commerce_order_item')->createFromPurchasableEntity($this->entity->getPurchasedEntity());

If a bug, provide steps to reproduce it from a clean install.

Change the form display of order items to hide quantity

Install commerce_ajax_atc module

🐛 Bug report
Status

Needs work

Version

2.0

Component

Cart

Created by

🇺🇸United States mglaman WI, USA

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

Comments & Activities

Production build 0.71.5 2024