class CartEventSubscriber in Commerce Core 8.2
Same name in this branch
- 8.2 modules/log/src/EventSubscriber/CartEventSubscriber.php \Drupal\commerce_log\EventSubscriber\CartEventSubscriber
- 8.2 modules/cart/src/EventSubscriber/CartEventSubscriber.php \Drupal\commerce_cart\EventSubscriber\CartEventSubscriber
- 8.2 modules/promotion/src/EventSubscriber/CartEventSubscriber.php \Drupal\commerce_promotion\EventSubscriber\CartEventSubscriber
Hierarchy
- class \Drupal\commerce_log\EventSubscriber\CartEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of CartEventSubscriber
File
- modules/
log/ src/ EventSubscriber/ CartEventSubscriber.php, line 11
Namespace
Drupal\commerce_log\EventSubscriberView source
class CartEventSubscriber implements EventSubscriberInterface {
/**
* The log storage.
*
* @var \Drupal\commerce_log\LogStorageInterface
*/
protected $logStorage;
/**
* Constructs a new CartEventSubscriber object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->logStorage = $entity_type_manager
->getStorage('commerce_log');
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events = [
CartEvents::CART_ENTITY_ADD => [
'onCartEntityAdd',
-100,
],
CartEvents::CART_ORDER_ITEM_REMOVE => [
'onCartOrderItemRemove',
-100,
],
];
return $events;
}
/**
* Creates a log when an entity has been added to the cart.
*
* @param \Drupal\commerce_cart\Event\CartEntityAddEvent $event
* The cart event.
*/
public function onCartEntityAdd(CartEntityAddEvent $event) {
$cart = $event
->getCart();
$this->logStorage
->generate($cart, 'cart_entity_added', [
'purchased_entity_label' => $event
->getOrderItem()
->label(),
])
->save();
}
/**
* Creates a log when an order item has been removed from the cart.
*
* @param \Drupal\commerce_cart\Event\CartOrderItemRemoveEvent $event
* The cart event.
*/
public function onCartOrderItemRemove(CartOrderItemRemoveEvent $event) {
$cart = $event
->getCart();
$this->logStorage
->generate($cart, 'cart_item_removed', [
'purchased_entity_label' => $event
->getOrderItem()
->label(),
])
->save();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CartEventSubscriber:: |
protected | property | The log storage. | |
CartEventSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
CartEventSubscriber:: |
public | function | Creates a log when an entity has been added to the cart. | |
CartEventSubscriber:: |
public | function | Creates a log when an order item has been removed from the cart. | |
CartEventSubscriber:: |
public | function | Constructs a new CartEventSubscriber object. |