Commit 75818064 authored by jeffam's avatar jeffam
Browse files

Issue #3255528 by jeffam, a-fro: Allow users to reset locked cart manually if...

Issue #3255528 by jeffam, a-fro: Allow users to reset locked cart manually if it contains an unlocked registration
parent 1e3aa46e
Loading
Loading
Loading
Loading
+53 −1
Original line number Diff line number Diff line
@@ -364,7 +364,11 @@ function erf_commerce_redirect_add_form_builder($entity_type, RegistrationType $
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function erf_commerce_form_registration_form_alter(&$form, FormStateInterface $form_state) {
  $registration = $form_state->get('registration');
  /** @var \Drupal\erf\Form\RegistrationForm $form_object */
  $form_object = $form_state->getFormObject();

  /** @var \Drupal\erf\Entity\RegistrationInterface $registration_entity */
  $registration = $form_object->getEntity();

  // Add a submit handler to redirect users to the cart if this registration
  // type is configured to do so.
@@ -376,6 +380,36 @@ function erf_commerce_form_registration_form_alter(&$form, FormStateInterface $f
  if ($registration->commerce_order_item_id->target_id) {
    $form['actions']['delete']['#access'] = FALSE;
  }

  $embedded = $form_object->getOperation() === 'embedded';
  $in_locked_cart = !$registration->commerce_order_item_id->isEmpty() && $registration->commerce_order_item_id->entity->getOrder()->isLocked();

  // Check if editing a registration linked to a locked cart and embedded in
  // another entity.
  if (!$registration->isNew() && $embedded && $in_locked_cart) {
    // Prevent saving the registration because it would trigger an attempt to
    // update the cart via erf_commerce_registration_update(). Locked carts
    // don't refresh the order properly.
    $form['actions']['submit']['#access'] = FALSE;

    $form['actions']['erf_commerce_locked_cart_reset'] = [
      '#type' => 'submit',
      '#value' => t('Reset cart'),
      '#submit' => ['erf_commerce_locked_cart_reset', 'erf_commerce_cart_redirect'],
    ];

    // Carts are locked when offsite payments are begun. We know of no other
    // reason why a cart would be locked, so the payment language here should
    // be fine.
    $form['erf_commerce_locked_cart_notice'] = [
      '#type' => 'inline_template',
      '#template' => '<div class="locked-cart-notice"><p>{{ message }}</p></div>',
      '#context' => [
        'message' => t('It looks like you started the payment process, which locked your cart. Did your payment succeed? If so, please contact customer support. Otherwise, reset your cart to continue.'),
      ],
      '#weight' => 2,
    ];
  }
}

/**
@@ -393,6 +427,24 @@ function erf_commerce_cart_redirect($form, FormStateInterface $form_state) {
  $form_state->setRedirect($cart_route);
}

/**
 * Submit function to handle the reset of a locked commerce cart for a
 * registration.
 *
 * @see erf_commerce_form_registration_form_alter().
 */
function erf_commerce_locked_cart_reset($form, FormStateInterface $form_state) {
  /** @var \Drupal\erf\Entity\RegistrationInterface $registration */
  $registration = $form_state->getFormObject()->getEntity();

  /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
  $order = $registration->commerce_order_item_id->entity->getOrder();

  // To reset a cart, it must be both unlocked and have its checkout step
  // changed from 'payment' to NULL.
  $order->set('checkout_step', '')->unlock()->save();
}

/**
 * Implements hook_ENTITY_TYPE_access().
 */