class OrderEventSubscriber in Commerce Core 8.2
Same name in this branch
- 8.2 modules/log/src/EventSubscriber/OrderEventSubscriber.php \Drupal\commerce_log\EventSubscriber\OrderEventSubscriber
- 8.2 modules/cart/src/EventSubscriber/OrderEventSubscriber.php \Drupal\commerce_cart\EventSubscriber\OrderEventSubscriber
- 8.2 modules/promotion/src/EventSubscriber/OrderEventSubscriber.php \Drupal\commerce_promotion\EventSubscriber\OrderEventSubscriber
Hierarchy
- class \Drupal\commerce_promotion\EventSubscriber\OrderEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of OrderEventSubscriber
1 string reference to 'OrderEventSubscriber'
- commerce_promotion.services.yml in modules/
promotion/ commerce_promotion.services.yml - modules/promotion/commerce_promotion.services.yml
1 service uses OrderEventSubscriber
File
- modules/
promotion/ src/ EventSubscriber/ OrderEventSubscriber.php, line 10
Namespace
Drupal\commerce_promotion\EventSubscriberView source
class OrderEventSubscriber implements EventSubscriberInterface {
/**
* The promotion storage.
*
* @var \Drupal\commerce_promotion\PromotionStorageInterface
*/
protected $promotionStorage;
/**
* The coupon storage.
*
* @var \Drupal\commerce_promotion\CouponStorageInterface
*/
protected $couponStorage;
/**
* The promotion usage.
*
* @var \Drupal\commerce_promotion\PromotionUsageInterface
*/
protected $usage;
/**
* Constructs a new OrderEventSubscriber object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\commerce_promotion\PromotionUsageInterface $usage
* The promotion usage.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, PromotionUsageInterface $usage) {
$this->promotionStorage = $entity_type_manager
->getStorage('commerce_promotion');
$this->couponStorage = $entity_type_manager
->getStorage('commerce_promotion_coupon');
$this->usage = $usage;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events = [
'commerce_order.place.pre_transition' => 'registerUsage',
];
return $events;
}
/**
* Registers promotion usage when the order is placed.
*
* @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
* The workflow transition event.
*/
public function registerUsage(WorkflowTransitionEvent $event) {
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
$order = $event
->getEntity();
$coupon_promotion_ids = [];
foreach ($order->coupons
->referencedEntities() as $coupon) {
/** @var \Drupal\commerce_promotion\Entity\CouponInterface $coupon */
$this->usage
->register($order, $coupon
->getPromotion(), $coupon);
$coupon_promotion_ids[] = $coupon
->getPromotionId();
}
$adjustments = $order
->collectAdjustments();
foreach ($adjustments as $adjustment) {
if ($adjustment
->getType() != 'promotion') {
continue;
}
$promotion_id = $adjustment
->getSourceId();
if ($promotion_id && !in_array($promotion_id, $coupon_promotion_ids)) {
$promotion = $this->promotionStorage
->load($promotion_id);
// Not every adjustment can be mapped to a promotion (because the
// the promotion was deleted, or because the adjustment is custom).
if ($promotion) {
$this->usage
->register($order, $promotion);
}
}
}
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
OrderEventSubscriber:: |
protected | property | The coupon storage. | |
OrderEventSubscriber:: |
protected | property | The promotion storage. | |
OrderEventSubscriber:: |
protected | property | The promotion usage. | |
OrderEventSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
OrderEventSubscriber:: |
public | function | Registers promotion usage when the order is placed. | |
OrderEventSubscriber:: |
public | function | Constructs a new OrderEventSubscriber object. |