You are here

public static function CouponRedemption::ajaxRefreshSummary in Commerce Core 8.2

Ajax callback for refreshing the order summary.

File

modules/promotion/src/Plugin/views/area/CouponRedemption.php, line 181

Class

CouponRedemption
Defines a coupon redemption area handler.

Namespace

Drupal\commerce_promotion\Plugin\views\area

Code

public static function ajaxRefreshSummary(array $form, FormStateInterface $form_state) {
  $triggering_element = $form_state
    ->getTriggeringElement();
  $parents = array_slice($triggering_element['#parents'], 0, -1);
  $inline_form = NestedArray::getValue($form, $parents);
  $order_storage = \Drupal::entityTypeManager()
    ->getStorage('commerce_order');

  /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
  $order = $order_storage
    ->load($inline_form['#configuration']['order_id']);
  $order_total = $order
    ->get('total_price')
    ->view([
    'label' => 'hidden',
    'type' => 'commerce_order_total_summary',
  ]);
  $order_total['#prefix'] = '<div data-drupal-selector="order-total-summary">';
  $order_total['#suffix'] = '</div>';
  $response = new AjaxResponse();
  if (isset($order_total)) {
    $response
      ->addCommand(new ReplaceCommand('[data-drupal-selector="order-total-summary"]', $order_total));
  }
  $response
    ->addCommand(new InsertCommand('[data-drupal-selector="' . $inline_form['#attributes']['data-drupal-selector'] . '"]', $inline_form));
  $response
    ->addCommand(new PrependCommand('[data-drupal-selector="' . $inline_form['#attributes']['data-drupal-selector'] . '"]', [
    '#type' => 'status_messages',
  ]));
  return $response;
}