Commit eb18a622 authored by git's avatar git Committed by Jonathan Sacksick
Browse files

Issue #3257792 by samuhe, jsacksick: Promotion Usage should provide a way to...

Issue #3257792 by samuhe, jsacksick: Promotion Usage should provide a way to delete individual usage.
parent 57c2ef81
Loading
Loading
Loading
Loading
+15 −1
Original line number Diff line number Diff line
@@ -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}
   */
@@ -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 = [];
+12 −0
Original line number Diff line number Diff line
@@ -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.
   *
+5 −0
Original line number Diff line number Diff line
@@ -96,6 +96,7 @@ class UsageTest extends OrderKernelTestBase {
   * Tests the usage API.
   *
   * @covers ::register
   * @covers ::unregister
   * @covers ::reassign
   * @covers ::delete
   * @covers ::deleteByCoupon
@@ -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.