Problem/Motivation

After checking out as a Guest, the next Guest cart is messed up:

  1. Product(s) from previous order remains in cart
  2. Yet Cart block shows "0 items"
  3. Hitting Checkout button on second cart brings user to "[step 4]. Complete" page with order id / confirmation of message of previous order.

Proposed resolution

  1. Set caching data/contexts on Cart page
  2. Track completed orders
    1. Similar to what was required in 1.x, 2.x CartProvider needs to track not only session orders but completed carts.
    2. Basically we just have "commerce_cart_orders" but we also need now need "commerce_cart_completed_orders"; CartProvider needs to be patched to respect these two. and finalizeCart needs to move it from commerce_cart_orders to commerce_cart_completed_orders.

Comments

steveoliver created an issue. See original summary.

steveoliver’s picture

https://github.com/drupalcommerce/commerce/pull/527/files shows failing test that should pass when this issue is resolved.

bojanz’s picture

Big thanks for the failing test!

steveoliver’s picture

Issue summary: View changes

Updating issue summary to include the child issue that was closed as a duplicate of this.

Lukas von Blarer’s picture

Status: Active » Needs review

Ok, going to review this on my installation in the coming days...

I guess this needs review?

Lukas von Blarer’s picture

Works for me so far! Thank you! Can't RTBC this tough. I am not familiar with 2.x yet...

mglaman’s picture

The tests and everything look good. I had one comment I proposed in the PR.

I think the conversion of a cart ID from the commerce_cart_orders session item to commerce_cart_completed_orders should be kept in finalizeCart, keeping deleteCartId as an actual deletion of the cart from the session. We can let commerce_cart_completed_orders persist and be something populated in finalizeCart

steveoliver’s picture

Makes sense. I refactored CartSession methods and usages to be explicit about the handling of active vs. completed carts: https://github.com/drupalcommerce/commerce/pull/527/commits/84714163.

bojanz’s picture

Title: Cart issues after Guest checkout » Previous carts persist after guest checkout

Retitling.

  • bojanz committed d7317f7 on 8.x-2.x authored by steveoliver
    Issue #2816059 by steveoliver, bojanz: Previous carts persist after...
bojanz’s picture

Status: Needs review » Fixed

Thought quite a bit about which API we wanted to have here.

Commerce 1.x had a $completed = FALSE argument for the regular set of functions, which controlled whether active or completed cart ids are manipulated. Boolean arguments are generally bad for DX, you need to know what the TRUE in hasCartId($cart_id, TRUE) stands for. So it made sense for steveoliver to introduce parallel methods, giving us an active and completed version of each method.
However, reviewing this with Matt it struck us that we now have code duplication, since methods only differ in the session key that they use.

In the end I managed to find a better alternative, a $type argument that uses a class constant. Hence we get this usage:

  // The cart is anonymous, move it to the 'completed' session.
    if (!$cart->getCustomerId()) {
      $this->cartSession->deleteCartId($cart->id(), CartSession::ACTIVE);
      $this->cartSession->addCartId($cart->id(), CartSession::COMPLETED);
    }

Which is clear, but still allows the implementation to stay lean.
$type defaults to self::ACTIVE by default, which also minimizes the BC break, making it almost non-existent.

Status: Fixed » Closed (fixed)

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