Loading modules/promotion/src/PromotionUsage.php +15 −1 Original line number Diff line number Diff line Loading @@ -41,6 +41,19 @@ class PromotionUsage implements PromotionUsageInterface { ->execute(); } /** * {@inheritdoc} */ public function unregister(OrderInterface $order, PromotionInterface $promotion, CouponInterface $coupon = NULL) { $query = $this->connection->delete('commerce_promotion_usage'); $query->condition('promotion_id', $promotion->id()); $query->condition('order_id', $order->id()); if ($coupon) { $query->condition('coupon_id', $coupon->id()); } $query->execute(); } /** * {@inheritdoc} */ Loading Loading @@ -101,7 +114,8 @@ class PromotionUsage implements PromotionUsageInterface { $query->condition('mail', $mail); } $query->groupBy('promotion_id'); $result = $query->execute()->fetchAllAssoc('promotion_id', \PDO::FETCH_ASSOC); $result = $query->execute() ->fetchAllAssoc('promotion_id', \PDO::FETCH_ASSOC); // Ensure that each promotion ID gets a count, even if it's not present // in the query due to non-existent usage. $counts = []; Loading modules/promotion/src/PromotionUsageInterface.php +12 −0 Original line number Diff line number Diff line Loading @@ -26,6 +26,18 @@ interface PromotionUsageInterface { */ public function register(OrderInterface $order, PromotionInterface $promotion, CouponInterface $coupon = NULL); /** * Unregisters usage for the given order. * * @param \Drupal\commerce_order\Entity\OrderInterface $order * The order. * @param \Drupal\commerce_promotion\Entity\PromotionInterface $promotion * The promotion. * @param \Drupal\commerce_promotion\Entity\CouponInterface $coupon * (Optional) The used coupon. */ public function unregister(OrderInterface $order, PromotionInterface $promotion, CouponInterface $coupon = NULL); /** * Reassigns usage to a new customer email. * Loading modules/promotion/tests/src/Kernel/UsageTest.php +5 −0 Original line number Diff line number Diff line Loading @@ -96,6 +96,7 @@ class UsageTest extends OrderKernelTestBase { * Tests the usage API. * * @covers ::register * @covers ::unregister * @covers ::reassign * @covers ::delete * @covers ::deleteByCoupon Loading Loading @@ -123,6 +124,10 @@ class UsageTest extends OrderKernelTestBase { $this->usage->register($order, $promotion); $this->assertEquals(1, $this->usage->load($promotion)); $this->usage->unregister($order, $promotion); $this->assertEquals(0, $this->usage->load($promotion)); $this->usage->register($order, $promotion); $this->usage->register($another_order, $promotion); $this->assertEquals(2, $this->usage->load($promotion)); // Test filtering by coupon. Loading Loading
modules/promotion/src/PromotionUsage.php +15 −1 Original line number Diff line number Diff line Loading @@ -41,6 +41,19 @@ class PromotionUsage implements PromotionUsageInterface { ->execute(); } /** * {@inheritdoc} */ public function unregister(OrderInterface $order, PromotionInterface $promotion, CouponInterface $coupon = NULL) { $query = $this->connection->delete('commerce_promotion_usage'); $query->condition('promotion_id', $promotion->id()); $query->condition('order_id', $order->id()); if ($coupon) { $query->condition('coupon_id', $coupon->id()); } $query->execute(); } /** * {@inheritdoc} */ Loading Loading @@ -101,7 +114,8 @@ class PromotionUsage implements PromotionUsageInterface { $query->condition('mail', $mail); } $query->groupBy('promotion_id'); $result = $query->execute()->fetchAllAssoc('promotion_id', \PDO::FETCH_ASSOC); $result = $query->execute() ->fetchAllAssoc('promotion_id', \PDO::FETCH_ASSOC); // Ensure that each promotion ID gets a count, even if it's not present // in the query due to non-existent usage. $counts = []; Loading
modules/promotion/src/PromotionUsageInterface.php +12 −0 Original line number Diff line number Diff line Loading @@ -26,6 +26,18 @@ interface PromotionUsageInterface { */ public function register(OrderInterface $order, PromotionInterface $promotion, CouponInterface $coupon = NULL); /** * Unregisters usage for the given order. * * @param \Drupal\commerce_order\Entity\OrderInterface $order * The order. * @param \Drupal\commerce_promotion\Entity\PromotionInterface $promotion * The promotion. * @param \Drupal\commerce_promotion\Entity\CouponInterface $coupon * (Optional) The used coupon. */ public function unregister(OrderInterface $order, PromotionInterface $promotion, CouponInterface $coupon = NULL); /** * Reassigns usage to a new customer email. * Loading
modules/promotion/tests/src/Kernel/UsageTest.php +5 −0 Original line number Diff line number Diff line Loading @@ -96,6 +96,7 @@ class UsageTest extends OrderKernelTestBase { * Tests the usage API. * * @covers ::register * @covers ::unregister * @covers ::reassign * @covers ::delete * @covers ::deleteByCoupon Loading Loading @@ -123,6 +124,10 @@ class UsageTest extends OrderKernelTestBase { $this->usage->register($order, $promotion); $this->assertEquals(1, $this->usage->load($promotion)); $this->usage->unregister($order, $promotion); $this->assertEquals(0, $this->usage->load($promotion)); $this->usage->register($order, $promotion); $this->usage->register($another_order, $promotion); $this->assertEquals(2, $this->usage->load($promotion)); // Test filtering by coupon. Loading