public function OrderEventSubscriber::registerUsage in Commerce Core 8.2
Registers promotion usage when the order is placed.
Parameters
\Drupal\state_machine\Event\WorkflowTransitionEvent $event: The workflow transition event.
File
- modules/
promotion/ src/ EventSubscriber/ OrderEventSubscriber.php, line 63
Class
Namespace
Drupal\commerce_promotion\EventSubscriberCode
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);
}
}
}
}