class AjaxAddToCartPopupSubscriber in Commerce Ajax Add to Cart 8
Event subscriber to display a popup when items are added to cart via AJAX.
Hierarchy
- class \Drupal\dc_ajax_add_cart_popup\EventSubscriber\AjaxAddToCartPopupSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of AjaxAddToCartPopupSubscriber
1 string reference to 'AjaxAddToCartPopupSubscriber'
- dc_ajax_add_cart_popup.services.yml in modules/
dc_ajax_add_cart_popup/ dc_ajax_add_cart_popup.services.yml - modules/dc_ajax_add_cart_popup/dc_ajax_add_cart_popup.services.yml
1 service uses AjaxAddToCartPopupSubscriber
- ajax_add_to_cart_popup_subscriber in modules/
dc_ajax_add_cart_popup/ dc_ajax_add_cart_popup.services.yml - \Drupal\dc_ajax_add_cart_popup\EventSubscriber\AjaxAddToCartPopupSubscriber
File
- modules/
dc_ajax_add_cart_popup/ src/ EventSubscriber/ AjaxAddToCartPopupSubscriber.php, line 18
Namespace
Drupal\dc_ajax_add_cart_popup\EventSubscriberView source
class AjaxAddToCartPopupSubscriber implements EventSubscriberInterface {
/**
* The entity that was added to the cart.
*
* @var \Drupal\commerce_product\Entity\ProductVariationInterface
*/
protected $purchasedEntity;
/**
* EntityTypeManager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a new AjaxAddToCartPopupSubscriber object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* Used to display the rendered product_variation entity.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* Adds the popup confirmation message on page.
*
* @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
* The response event.
*/
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);
}
/**
* Initializes the purchased entity.
*
* @param \Drupal\commerce_cart\Event\CartEntityAddEvent $event
* The add to cart event.
*/
public function onAddToCart(CartEntityAddEvent $event) {
$this->purchasedEntity = $event
->getEntity();
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
KernelEvents::RESPONSE => 'onResponse',
CartEvents::CART_ENTITY_ADD => 'onAddToCart',
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AjaxAddToCartPopupSubscriber:: |
protected | property | EntityTypeManager service. | |
AjaxAddToCartPopupSubscriber:: |
protected | property | The entity that was added to the cart. | |
AjaxAddToCartPopupSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
AjaxAddToCartPopupSubscriber:: |
public | function | Initializes the purchased entity. | |
AjaxAddToCartPopupSubscriber:: |
public | function | Adds the popup confirmation message on page. | |
AjaxAddToCartPopupSubscriber:: |
public | function | Constructs a new AjaxAddToCartPopupSubscriber object. |