Skip to content
Snippets Groups Projects
Commit 60504969 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 59fb4f5d
Branches
Tags
2 merge requests!235Issue #3115150 by mbovan: Submitting add to cart form with empty quantity...,!205Issue #3349465 by tBKoT, jsacksick, bojanz: Update several VAT rates and setup...
......@@ -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 = [];
......
......@@ -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.
*
......
......@@ -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.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment