You are here

public function AjaxAddToCartPopupSubscriber::onResponse in Commerce Ajax Add to Cart 8

Adds the popup confirmation message on page.

Parameters

\Symfony\Component\HttpKernel\Event\FilterResponseEvent $event: The response event.

File

modules/dc_ajax_add_cart_popup/src/EventSubscriber/AjaxAddToCartPopupSubscriber.php, line 50

Class

AjaxAddToCartPopupSubscriber
Event subscriber to display a popup when items are added to cart via AJAX.

Namespace

Drupal\dc_ajax_add_cart_popup\EventSubscriber

Code

public function onResponse(FilterResponseEvent $event) {
  $response = $event
    ->getResponse();

  // We only care if this happened after an entity was added to the cart.
  if (!$this->purchasedEntity) {
    return;
  }

  // We only care about AJAX responses.
  if (!$response instanceof AjaxResponse) {
    return;
  }

  // Render the status message and the entity.
  $view_builder = $this->entityTypeManager
    ->getViewBuilder('commerce_product_variation');
  $product_variation = $view_builder
    ->view($this->purchasedEntity, 'dc_ajax_add_to_cart_popup');
  $content = [
    '#theme' => 'dc_ajax_add_cart_popup',
    '#product_variation' => $product_variation,
    '#product_variation_entity' => $this->purchasedEntity,
    '#cart_url' => Url::fromRoute('commerce_cart.page')
      ->toString(),
  ];
  $title = '';
  $options = [
    'width' => '700',
  ];
  $response
    ->addCommand(new OpenModalDialogCommand($title, $content, $options));
  $event
    ->setResponse($response);
}