Loading commerce_promotion_giveaway.services.yml +5 −0 Original line number Diff line number Diff line Loading @@ -3,3 +3,8 @@ services: class: Drupal\commerce_promotion_giveaway\GiveawayPromotionOrderProcessor tags: - { name: commerce_order.order_processor, priority: 99 } commerce_promotion_giveaway.order_subscriber: class: Drupal\commerce_promotion_giveaway\EventSubscriber\GiveawayOrderEventSubscriber arguments: [ '@entity_type.manager', '@commerce_promotion.usage' ] tags: - { name: event_subscriber } src/EventSubscriber/GiveawayOrderEventSubscriber.php 0 → 100644 +70 −0 Original line number Diff line number Diff line <?php namespace Drupal\commerce_promotion_giveaway\EventSubscriber; use Drupal\commerce_promotion\Entity\PromotionInterface; use Drupal\commerce_promotion\PromotionUsageInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Drupal\state_machine\Event\WorkflowTransitionEvent; class GiveawayOrderEventSubscriber implements EventSubscriberInterface { /** * The promotion storage. * * @var \Drupal\commerce_promotion\PromotionStorageInterface */ protected $promotionStorage; /** * The promotion usage. * * @var \Drupal\commerce_promotion\PromotionUsageInterface */ protected $usage; /** * Constructs a new GiveawayOrderEventSubscriber 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->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(); foreach ($order->getItems() as $item) { $promotion_id = $item->getData('giveaway_not_adjusted'); if ($promotion_id) { $promotion = $this->promotionStorage->load($promotion_id); if ($promotion instanceof PromotionInterface) { $this->usage->register($order, $promotion); } } } } } src/Plugin/Commerce/PromotionOffer/Giveaway.php +6 −0 Original line number Diff line number Diff line Loading @@ -209,6 +209,12 @@ class Giveaway extends OrderPromotionOfferBase { // Should the price be visible for the user. if (!$this->configuration['show_price']) { $unit_price = $unit_price->multiply((string) 0); // If the order item price is set to zero no adjustment will be added. // The promotion usage will therefore not increase. // We set an indicator on the order item that the promotion was used // but not adjusted to register the promotion usage in // GiveawayOrderEventSubscriber. $order_item->setData('giveaway_not_adjusted', $promotion->id()); } // Setting the price, if price was set to 0 the overridden tag gets set. Loading tests/src/Kernel/GiveawayPromotionTest.php +6 −0 Original line number Diff line number Diff line Loading @@ -346,6 +346,12 @@ class GiveawayPromotionTest extends CommerceKernelTestBase { 20, $order->getTotalPrice()->getNumber(), 'The order total has not changed.'); self::assertEquals( $this->promotion->id(), $order_item->getData('promotion_not_adjusted'), 'The promotion was stored on the order item to register usage.' ); } /** Loading Loading
commerce_promotion_giveaway.services.yml +5 −0 Original line number Diff line number Diff line Loading @@ -3,3 +3,8 @@ services: class: Drupal\commerce_promotion_giveaway\GiveawayPromotionOrderProcessor tags: - { name: commerce_order.order_processor, priority: 99 } commerce_promotion_giveaway.order_subscriber: class: Drupal\commerce_promotion_giveaway\EventSubscriber\GiveawayOrderEventSubscriber arguments: [ '@entity_type.manager', '@commerce_promotion.usage' ] tags: - { name: event_subscriber }
src/EventSubscriber/GiveawayOrderEventSubscriber.php 0 → 100644 +70 −0 Original line number Diff line number Diff line <?php namespace Drupal\commerce_promotion_giveaway\EventSubscriber; use Drupal\commerce_promotion\Entity\PromotionInterface; use Drupal\commerce_promotion\PromotionUsageInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Drupal\state_machine\Event\WorkflowTransitionEvent; class GiveawayOrderEventSubscriber implements EventSubscriberInterface { /** * The promotion storage. * * @var \Drupal\commerce_promotion\PromotionStorageInterface */ protected $promotionStorage; /** * The promotion usage. * * @var \Drupal\commerce_promotion\PromotionUsageInterface */ protected $usage; /** * Constructs a new GiveawayOrderEventSubscriber 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->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(); foreach ($order->getItems() as $item) { $promotion_id = $item->getData('giveaway_not_adjusted'); if ($promotion_id) { $promotion = $this->promotionStorage->load($promotion_id); if ($promotion instanceof PromotionInterface) { $this->usage->register($order, $promotion); } } } } }
src/Plugin/Commerce/PromotionOffer/Giveaway.php +6 −0 Original line number Diff line number Diff line Loading @@ -209,6 +209,12 @@ class Giveaway extends OrderPromotionOfferBase { // Should the price be visible for the user. if (!$this->configuration['show_price']) { $unit_price = $unit_price->multiply((string) 0); // If the order item price is set to zero no adjustment will be added. // The promotion usage will therefore not increase. // We set an indicator on the order item that the promotion was used // but not adjusted to register the promotion usage in // GiveawayOrderEventSubscriber. $order_item->setData('giveaway_not_adjusted', $promotion->id()); } // Setting the price, if price was set to 0 the overridden tag gets set. Loading
tests/src/Kernel/GiveawayPromotionTest.php +6 −0 Original line number Diff line number Diff line Loading @@ -346,6 +346,12 @@ class GiveawayPromotionTest extends CommerceKernelTestBase { 20, $order->getTotalPrice()->getNumber(), 'The order total has not changed.'); self::assertEquals( $this->promotion->id(), $order_item->getData('promotion_not_adjusted'), 'The promotion was stored on the order item to register usage.' ); } /** Loading