modules/acm_cart/src/Form/CustomerCartForm.php
private function updateCart(FormStateInterface $form_state) {
    try {
      $cart = $this->cartStorage->updateCart();
      $response_message = $cart->get('response_message');
      // We will have type of message like error or success. key '0' contains
      // the response message string while key '1' contains the response
      // message context/type like success or coupon.
      if (!empty($response_message[1])) {
        // If its success.
        if ($response_message[1] == 'success') {
          $this->successMessage = $response_message[0];
        }
        elseif ($response_message[1] == 'error_coupon') {
          // Set the error and require rebuild.
          $form_state->setErrorByName('coupon', $response_message[0]);
          $form_state->setRebuild(TRUE);

          // Remove the coupon and update the cart.
          $this->cartStorage->setCoupon('');
          $this->updateCart($form_state);
        }
      }
    }
    catch (\Exception $e) {
      if (acm_is_exception_api_down_exception($e)) {
        $this->messenger()->addError($e->getMessage());
        $form_state->setErrorByName('custom', $e->getMessage());
        $form_state->setRebuild(TRUE);
      }

      // Dispatch event so action can be taken.
      $dispatcher = \Drupal::service('event_dispatcher');
      $event = new UpdateCartErrorEvent($e);
      $dispatcher->dispatch(UpdateCartErrorEvent::SUBMIT, $event);
      $this->messenger()->addError($e->getMessage());
    }
  }
private function updateCart(FormStateInterface $form_state) {
    try {
      $cart = $this->cartStorage->updateCart();
      $response_message = $cart->get('response_message');
      // We will have type of message like error or success. key '0' contains
      // the response message string while key '1' contains the response
      // message context/type like success or coupon.
      if (!empty($response_message[1])) {
        // If its success.
        if ($response_message[1] == 'success') {
          $this->successMessage = $response_message[0];
        }
        elseif ($response_message[1] == 'error_coupon') {
          // Set the error and require rebuild.
          $form_state->setErrorByName('coupon', $response_message[0]);
          $form_state->setRebuild(TRUE);

          // Remove the coupon and update the cart.
          $this->cartStorage->setCoupon('');
          $this->updateCart($form_state);
        }
      }
    }
    catch (\Exception $e) {
      if (acm_is_exception_api_down_exception($e)) {
        $this->messenger()->addError($e->getMessage());
        $form_state->setErrorByName('custom', $e->getMessage());
        $form_state->setRebuild(TRUE);
      }

      // Dispatch event so action can be taken.
      $dispatcher = \Drupal::service('event_dispatcher');
      $event = new UpdateCartErrorEvent($e);
      $dispatcher->dispatch(UpdateCartErrorEvent::SUBMIT, $event);
      $this->messenger()->addError($e->getMessage());
    }
  }
private function updateCart ( FormStateInterface $ form_state ) { try { $ cart = $ this -> cartStorage -> updateCart ( ) ; $ response_message = $ cart -> get ( 'response_message' ) ; // We will have type of message like error or success. key '0' contains // the response message string while key '1' contains the response // message context/type like success or coupon. if ( ! empty ( $ response_message [ 1 ] ) ) { // If its success. if ( $ response_message [ 1 ] == 'success' ) { $ this -> successMessage = $ response_message [ 0 ] ; } elseif ( $ response_message [ 1 ] == 'error_coupon' ) { // Set the error and require rebuild. $ form_state -> setErrorByName ( 'coupon' , $ response_message [ 0 ] ) ; $ form_state -> setRebuild ( TRUE ) ; // Remove the coupon and update the cart. $ this -> cartStorage -> setCoupon ( '' ) ; $ this -> updateCart ( $ form_state ) ; } } } catch ( \ Exception $ e ) { if ( acm_is_exception_api_down_exception ( $ e ) ) { $ this -> messenger ( ) -> addError ( $ e -> getMessage ( ) ) ; $ form_state -> setErrorByName ( 'custom' , $ e -> getMessage ( ) ) ; $ form_state -> setRebuild ( TRUE ) ; } // Dispatch event so action can be taken. $ dispatcher = \ Drupal :: service ( 'event_dispatcher' ) ; $ event = new UpdateCartErrorEvent ( $ e ) ; $ dispatcher -> dispatch ( UpdateCartErrorEvent :: SUBMIT , $ event ) ; $ this -> messenger ( ) -> addError ( $ e -> getMessage ( ) ) ; } }
Cart update utility.

@param \Drupal\Core\Form\FormStateInterface $form_state
FormStateInterface object.
Cart update utility .