CartEventSubscriber.php in Commerce Core 8.2
File
modules/log/src/EventSubscriber/CartEventSubscriber.php
View source
<?php
namespace Drupal\commerce_log\EventSubscriber;
use Drupal\commerce_cart\Event\CartEntityAddEvent;
use Drupal\commerce_cart\Event\CartEvents;
use Drupal\commerce_cart\Event\CartOrderItemRemoveEvent;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CartEventSubscriber implements EventSubscriberInterface {
protected $logStorage;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->logStorage = $entity_type_manager
->getStorage('commerce_log');
}
public static function getSubscribedEvents() {
$events = [
CartEvents::CART_ENTITY_ADD => [
'onCartEntityAdd',
-100,
],
CartEvents::CART_ORDER_ITEM_REMOVE => [
'onCartOrderItemRemove',
-100,
],
];
return $events;
}
public function onCartEntityAdd(CartEntityAddEvent $event) {
$cart = $event
->getCart();
$this->logStorage
->generate($cart, 'cart_entity_added', [
'purchased_entity_label' => $event
->getOrderItem()
->label(),
])
->save();
}
public function onCartOrderItemRemove(CartOrderItemRemoveEvent $event) {
$cart = $event
->getCart();
$this->logStorage
->generate($cart, 'cart_item_removed', [
'purchased_entity_label' => $event
->getOrderItem()
->label(),
])
->save();
}
}