Commit d41560f45bd for woocommerce
commit d41560f45bd1562273eb705536be077633b91ff8
Author: Luigi Teschio <gigitux@gmail.com>
Date: Mon Nov 23 12:05:05 2026 +0100
Count coupon uses across account and billing emails (#71271)
Forwardport of 528-gh-Automattic/woocommerce.
Source PR head: 8be10594de61e0c1f71fa1860cc1cd88f84f417f
Co-authored-by: Oluwaseun Olorunsola <seun.olorunsola@automattic.com>
diff --git a/plugins/woocommerce/includes/abstracts/abstract-wc-order.php b/plugins/woocommerce/includes/abstracts/abstract-wc-order.php
index 97c823cb526..477dcecc3a5 100644
--- a/plugins/woocommerce/includes/abstracts/abstract-wc-order.php
+++ b/plugins/woocommerce/includes/abstracts/abstract-wc-order.php
@@ -1855,7 +1855,13 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
$user_ids_and_emails = $this->get_billing_and_current_user_ids_and_aliases( $billing_email );
}
- $held_key_for_user = $this->hold_coupon_for_users( $coupon, $user_ids_and_emails, $user_alias );
+ $coupon_data_store = $coupon->get_data_store();
+ $coupon_aliases = $user_ids_and_emails;
+ if ( $coupon_data_store instanceof WC_Data_Store && $coupon_data_store->has_callable( 'add_recorded_user_ids_to_aliases' ) ) {
+ // @phpstan-ignore-next-line method.notFound (Guarded by has_callable() and called via __call() on the coupon data store instance.)
+ $coupon_aliases = $coupon_data_store->add_recorded_user_ids_to_aliases( $coupon, $user_ids_and_emails );
+ }
+ $held_key_for_user = $this->hold_coupon_for_users( $coupon, $coupon_aliases, $user_alias );
if ( $held_key_for_user ) {
$held_keys_for_user[ $coupon->get_id() ] = $held_key_for_user;
@@ -1922,19 +1928,32 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
/**
* Helper method to get all aliases and IDs for current user and provided billing email.
*
+ * A logged in customer's uses and holds are recorded under their user ID, so the list includes the current
+ * user's ID and the ID of the account that owns each email.
+ *
* @param string $billing_email Billing email to look up for.
*
* @return array Array of all aliases and IDs.
* @throws Exception When validation fails.
*/
private function get_billing_and_current_user_ids_and_aliases( $billing_email ): array {
- $emails = array( $billing_email );
+ $emails = array( $billing_email );
+ $user_ids = array();
if ( get_current_user_id() ) {
- $emails[] = wp_get_current_user()->user_email;
+ $emails[] = wp_get_current_user()->user_email;
+ $user_ids[] = get_current_user_id();
}
$emails = array_unique( array_map( 'strtolower', array_map( 'sanitize_email', $emails ) ) );
- $user_ids = wc_get_container()->get( CustomersSearchService::class )->find_user_ids_by_billing_email_for_coupons_usage_lookup( $emails );
- return array_merge( $user_ids, $emails );
+ $user_ids = array_merge( $user_ids, wc_get_container()->get( CustomersSearchService::class )->find_user_ids_by_billing_email_for_coupons_usage_lookup( $emails ) );
+
+ foreach ( array_filter( $emails ) as $email ) {
+ $user = get_user_by( 'email', $email );
+ if ( $user ) {
+ $user_ids[] = $user->ID;
+ }
+ }
+
+ return array_merge( array_unique( array_map( 'intval', $user_ids ) ), $emails );
}
/**
@@ -2115,7 +2134,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
$identities = array();
foreach ( $coupons as $coupon ) {
- $coupon->increase_usage_count( $used_by );
+ $coupon->increase_usage_count( $used_by, $this instanceof WC_Order ? $this : null );
if ( $coupon->get_id() ) {
$identities[ $coupon->get_id() ] = $used_by;
}
diff --git a/plugins/woocommerce/includes/class-wc-cart.php b/plugins/woocommerce/includes/class-wc-cart.php
index 63a8507ed9e..e79f0412585 100644
--- a/plugins/woocommerce/includes/class-wc-cart.php
+++ b/plugins/woocommerce/includes/class-wc-cart.php
@@ -2058,8 +2058,29 @@ class WC_Cart extends WC_Legacy_Cart {
// For guest, usage per user has not been enforced yet. Enforce it now.
$coupon_data_store = $coupon->get_data_store();
$billing_email = strtolower( sanitize_email( $billing_email ) );
- if ( $coupon_data_store && $coupon_data_store->get_usage_by_email( $coupon, $billing_email ) >= $coupon_usage_limit ) {
- if ( $coupon_data_store->get_tentative_usages_for_user( $coupon->get_id(), array( $billing_email ) ) ) {
+ // A use by the account that owns this email is recorded under its user ID, not the email.
+ $email_owner = $billing_email ? get_user_by( 'email', $billing_email ) : false;
+ $aliases = array_filter( array( $billing_email, $email_owner ? $email_owner->ID : 0 ) );
+ $uses_alias = $coupon_data_store instanceof WC_Data_Store && $coupon_data_store->has_callable( 'get_usage_by_aliases' );
+ $usage_count = 0;
+ if ( $coupon_data_store ) {
+ $usage_count = (int) $coupon_data_store->get_usage_by_email( $coupon, $billing_email );
+ if ( $email_owner ) {
+ // @phpstan-ignore-next-line method.notFound (Part of WC_Coupon_Data_Store_Interface, called via __call() on the coupon data store instance.)
+ $usage_count += (int) $coupon_data_store->get_usage_by_user_id( $coupon, $email_owner->ID );
+ }
+ }
+ // The larger count keeps a custom store's get_usage_by_email() override in effect.
+ if ( $uses_alias ) {
+ // @phpstan-ignore-next-line method.notFound (Guarded by has_callable() and called via __call() on the coupon data store instance.)
+ $usage_count = max( $usage_count, (int) $coupon_data_store->get_usage_by_aliases( $coupon, $aliases ) );
+ }
+ if ( $coupon_data_store && $usage_count >= $coupon_usage_limit ) {
+ if ( $uses_alias ) {
+ // @phpstan-ignore-next-line method.notFound (Guarded by has_callable() and called via __call() on the coupon data store instance.)
+ $aliases = $coupon_data_store->add_recorded_user_ids_to_aliases( $coupon, $aliases );
+ }
+ if ( $coupon_data_store->get_tentative_usages_for_user( $coupon->get_id(), $aliases ) ) {
$coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_USAGE_LIMIT_COUPON_STUCK_GUEST );
} else {
$coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_USAGE_LIMIT_REACHED );
diff --git a/plugins/woocommerce/includes/class-wc-privacy-erasers.php b/plugins/woocommerce/includes/class-wc-privacy-erasers.php
index bd0f2eb8385..d7bc03f0316 100644
--- a/plugins/woocommerce/includes/class-wc-privacy-erasers.php
+++ b/plugins/woocommerce/includes/class-wc-privacy-erasers.php
@@ -30,6 +30,16 @@ class WC_Privacy_Erasers {
$user = get_user_by( 'email', $email_address ); // Check if user has an ID in the DB to load stored personal data.
+ $coupon_data_store = WC_Data_Store::load( 'coupon' );
+ if ( $coupon_data_store->has_callable( 'remove_used_by_email_records' ) ) {
+ // @phpstan-ignore-next-line method.notFound (WC_Data_Store forwards the call to the coupon data store.)
+ $removed = $coupon_data_store->remove_used_by_email_records( (string) $email_address, $user instanceof WP_User ? $user->ID : 0 );
+ if ( $removed ) {
+ $response['messages'][] = __( 'Removed customer email from coupon usage records', 'woocommerce' );
+ $response['items_removed'] = true;
+ }
+ }
+
if ( ! $user instanceof WP_User ) {
return $response;
}
diff --git a/plugins/woocommerce/includes/data-stores/class-wc-coupon-data-store-cpt.php b/plugins/woocommerce/includes/data-stores/class-wc-coupon-data-store-cpt.php
index 851e114ae93..f50fb3d7da6 100644
--- a/plugins/woocommerce/includes/data-stores/class-wc-coupon-data-store-cpt.php
+++ b/plugins/woocommerce/includes/data-stores/class-wc-coupon-data-store-cpt.php
@@ -53,6 +53,7 @@ class WC_Coupon_Data_Store_CPT extends WC_Data_Store_WP implements WC_Coupon_Dat
'maximum_amount',
'customer_email',
'_used_by',
+ '_used_by_email',
'_edit_lock',
'_edit_last',
);
@@ -410,6 +411,7 @@ class WC_Coupon_Data_Store_CPT extends WC_Data_Store_WP implements WC_Coupon_Dat
if ( $used_by ) {
$this->add_coupon_used_by( $coupon, $used_by, $coupon_held_key_for_user );
+ $this->add_used_by_emails( $coupon, $used_by, $order );
$coupon->set_used_by( (array) get_post_meta( $coupon->get_id(), '_used_by' ) );
}
@@ -450,6 +452,205 @@ class WC_Coupon_Data_Store_CPT extends WC_Data_Store_WP implements WC_Coupon_Dat
$this->refresh_coupon_data( $coupon );
}
+ /**
+ * Record the emails behind a use counted under a user ID, as `<user id>:<email>` rows.
+ *
+ * A use by a logged in customer is stored as their user ID only. Recording the account email and the
+ * order's billing email at the time of the use lets per-user limits still find it after the customer
+ * edits either one.
+ *
+ * @param WC_Coupon $coupon Coupon object.
+ * @param string|int $used_by User ID or billing email the use was counted under.
+ * @param WC_Order|null $order Order the use belongs to, if known.
+ * @return void
+ */
+ private function add_used_by_emails( $coupon, $used_by, $order = null ) {
+ $user_id = is_numeric( $used_by ) ? (int) $used_by : 0;
+ if ( 0 >= $user_id || 0 >= $coupon->get_usage_limit_per_user() ) {
+ return;
+ }
+
+ $emails = array();
+ $user = get_userdata( $user_id );
+ if ( $user ) {
+ $emails[] = $user->user_email;
+ }
+ if ( $order instanceof WC_Order ) {
+ $emails[] = $order->get_billing_email();
+ }
+ $emails = array_unique( array_filter( array_map( 'strtolower', array_map( 'sanitize_email', $emails ) ) ) );
+
+ $existing = (array) get_post_meta( $coupon->get_id(), '_used_by_email', false );
+ foreach ( $emails as $email ) {
+ $value = $user_id . ':' . $email;
+ if ( ! in_array( $value, $existing, true ) ) {
+ add_post_meta( $coupon->get_id(), '_used_by_email', $value );
+ }
+ }
+
+ $this->refresh_coupon_data( $coupon );
+ }
+
+ /**
+ * Remove the email records of a user once none of their uses remain on the coupon.
+ *
+ * @param WC_Coupon $coupon Coupon object.
+ * @param string|int $used_by User ID or billing email the use was counted under.
+ * @return void
+ */
+ private function maybe_remove_used_by_emails( $coupon, $used_by ) {
+ global $wpdb;
+
+ $user_id = is_numeric( $used_by ) ? (int) $used_by : 0;
+ if ( 0 >= $user_id ) {
+ return;
+ }
+
+ $remaining_uses = (int) $wpdb->get_var(
+ $wpdb->prepare(
+ "SELECT COUNT( meta_id ) FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = '_used_by' AND meta_value = %s;",
+ $coupon->get_id(),
+ (string) $user_id
+ )
+ );
+ if ( 0 < $remaining_uses ) {
+ return;
+ }
+
+ $wpdb->query(
+ $wpdb->prepare(
+ "DELETE FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = '_used_by_email' AND meta_value LIKE %s;",
+ $coupon->get_id(),
+ $wpdb->esc_like( $user_id . ':' ) . '%'
+ )
+ );
+ $this->refresh_coupon_data( $coupon );
+ }
+
+ /**
+ * Add the user IDs whose recorded uses of the coupon were tied to any of the emails in a list of aliases.
+ *
+ * @since 11.4.0
+ * @param WC_Coupon $coupon Coupon object.
+ * @param array $aliases User IDs and emails.
+ * @return array The aliases plus any recorded user IDs, without duplicates.
+ */
+ public function add_recorded_user_ids_to_aliases( $coupon, array $aliases ): array {
+ global $wpdb;
+
+ $emails = array_values(
+ array_unique(
+ array_filter(
+ array_map(
+ fn( $alias ) => strtolower( sanitize_email( (string) $alias ) ),
+ array_filter( $aliases, fn( $alias ) => ! is_numeric( $alias ) )
+ )
+ )
+ )
+ );
+ if ( ! $emails || ! $coupon->get_id() ) {
+ return array_values( array_unique( array_filter( $aliases ) ) );
+ }
+
+ // LIKE instead of SUBSTRING_INDEX keeps this portable to the SQLite integration.
+ $conditions = implode( ' OR ', array_fill( 0, count( $emails ), 'meta_value LIKE %s' ) );
+ $patterns = array_map( fn( $email ) => '%:' . $wpdb->esc_like( $email ), $emails );
+ $records = $wpdb->get_col(
+ $wpdb->prepare(
+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- The condition list is built from the count of emails.
+ "SELECT meta_value FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = '_used_by_email' AND ( $conditions );",
+ array_merge( array( $coupon->get_id() ), $patterns )
+ )
+ );
+
+ foreach ( $records as $record ) {
+ list( $user_id, $email ) = array_pad( explode( ':', (string) $record, 2 ), 2, '' );
+ if ( 0 < (int) $user_id && in_array( $email, $emails, true ) ) {
+ $aliases[] = (int) $user_id;
+ }
+ }
+
+ return array_values( array_unique( array_filter( $aliases ) ) );
+ }
+
+ /**
+ * Remove the email records of a person from all coupons.
+ *
+ * Removes records holding the email, and all records of the user ID when one is given.
+ *
+ * @since 11.4.0
+ * @param string $email Email address.
+ * @param int $user_id User ID, or 0 when the email has no account.
+ * @return int Number of records removed.
+ */
+ public function remove_used_by_email_records( string $email, int $user_id = 0 ): int {
+ global $wpdb;
+
+ $email = strtolower( sanitize_email( $email ) );
+ $patterns = array();
+ if ( $email ) {
+ $patterns[] = '%:' . $wpdb->esc_like( $email );
+ }
+ if ( 0 < $user_id ) {
+ $patterns[] = $wpdb->esc_like( $user_id . ':' ) . '%';
+ }
+ if ( ! $patterns ) {
+ return 0;
+ }
+
+ $conditions = implode( ' OR ', array_fill( 0, count( $patterns ), 'meta_value LIKE %s' ) );
+ $records = $wpdb->get_results(
+ $wpdb->prepare(
+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- The condition list is built from the count of patterns.
+ "SELECT meta_id, post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = '_used_by_email' AND ( $conditions );",
+ $patterns
+ )
+ );
+
+ $removed = 0;
+ foreach ( $records as $record ) {
+ list( $record_user_id, $record_email ) = array_pad( explode( ':', (string) $record->meta_value, 2 ), 2, '' );
+ $matches_user = 0 < $user_id && (int) $record_user_id === $user_id;
+ $matches_email = $email && strtolower( $record_email ) === $email;
+ if ( ( $matches_user || $matches_email ) && delete_metadata_by_mid( 'post', (int) $record->meta_id ) ) {
+ wp_cache_delete( WC_Data::generate_meta_cache_key( (int) $record->post_id, 'coupons' ), 'coupons' );
+ ++$removed;
+ }
+ }
+
+ return $removed;
+ }
+
+ /**
+ * Get the number of uses and current holds of a coupon by a list of user IDs and emails.
+ *
+ * Emails also match uses counted under a user ID when that use was recorded with the email.
+ *
+ * @since 11.4.0
+ * @param WC_Coupon $coupon Coupon object.
+ * @param array $aliases User IDs and emails.
+ * @return int
+ */
+ public function get_usage_by_aliases( $coupon, array $aliases ): int {
+ global $wpdb;
+
+ $aliases = array_map( 'strval', $this->add_recorded_user_ids_to_aliases( $coupon, $aliases ) );
+ if ( ! $aliases ) {
+ return 0;
+ }
+
+ $placeholders = implode( ', ', array_fill( 0, count( $aliases ), '%s' ) );
+ $usage_count = (int) $wpdb->get_var(
+ $wpdb->prepare(
+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- The placeholder list is built from the count of aliases.
+ "SELECT COUNT( meta_id ) FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = '_used_by' AND meta_value IN ( $placeholders );",
+ array_merge( array( $coupon->get_id() ), $aliases )
+ )
+ );
+
+ return $usage_count + (int) $this->get_tentative_usages_for_user( $coupon->get_id(), $aliases );
+ }
+
/**
* Decrease usage count for current coupon.
*
@@ -475,6 +676,7 @@ class WC_Coupon_Data_Store_CPT extends WC_Data_Store_WP implements WC_Coupon_Dat
);
if ( $meta_id ) {
delete_metadata_by_mid( 'post', $meta_id );
+ $this->maybe_remove_used_by_emails( $coupon, $used_by );
$coupon->set_used_by( (array) get_post_meta( $coupon->get_id(), '_used_by' ) );
$this->refresh_coupon_data( $coupon );
}
diff --git a/plugins/woocommerce/src/StoreApi/Utilities/OrderController.php b/plugins/woocommerce/src/StoreApi/Utilities/OrderController.php
index a67947e8da9..521a88f1e0c 100644
--- a/plugins/woocommerce/src/StoreApi/Utilities/OrderController.php
+++ b/plugins/woocommerce/src/StoreApi/Utilities/OrderController.php
@@ -1101,6 +1101,11 @@ class OrderController {
*/
private function get_usage_per_aliases( $coupon, $aliases ) {
global $wpdb;
+ $data_store = $coupon->get_data_store();
+ if ( $data_store instanceof \WC_Data_Store && $data_store->has_callable( 'add_recorded_user_ids_to_aliases' ) ) {
+ // @phpstan-ignore-next-line method.notFound (Guarded by has_callable() and called via __call() on the coupon data store instance.)
+ $aliases = $data_store->add_recorded_user_ids_to_aliases( $coupon, $aliases );
+ }
$aliases = array_unique( array_filter( $aliases ) );
$aliases_string = "('" . implode( "','", array_map( 'esc_sql', $aliases ) ) . "')";
$usage_count = $wpdb->get_var(
@@ -1111,7 +1116,6 @@ class OrderController {
)
);
- $data_store = $coupon->get_data_store();
// Coupons can be held for an x amount of time before being applied to an order, so we need to check if it's already being held in (maybe via another flow).
$tentative_usage_count = $data_store->get_tentative_usages_for_user( $coupon->get_id(), $aliases );
return $tentative_usage_count + $usage_count;
diff --git a/plugins/woocommerce/tests/php/includes/abstracts/class-wc-abstract-order-test.php b/plugins/woocommerce/tests/php/includes/abstracts/class-wc-abstract-order-test.php
index f053293a9d3..2e7e71604e9 100644
--- a/plugins/woocommerce/tests/php/includes/abstracts/class-wc-abstract-order-test.php
+++ b/plugins/woocommerce/tests/php/includes/abstracts/class-wc-abstract-order-test.php
@@ -3099,4 +3099,79 @@ class WC_Abstract_Order_Test extends WC_Unit_Test_Case {
$this->assertFalse( $result );
$this->assertTrue( $order->update_status( OrderStatus::PROCESSING ), 'update_status() should return true once the save succeeds.' );
}
+
+ /**
+ * Try to hold the per-user coupon allowance for a new, unsaved order, as the checkout does.
+ *
+ * @param string $code Coupon code in the cart.
+ * @param string $billing_email Billing email of the checkout.
+ * @return string|null The error message, or null when the hold was taken.
+ */
+ private function try_hold_coupon( string $code, string $billing_email ): ?string {
+ WC()->cart->set_applied_coupons( array( $code ) );
+ $order = new WC_Order();
+ $order->set_billing_email( $billing_email );
+ try {
+ $order->hold_applied_coupons( $billing_email );
+ return null;
+ } catch ( Exception $e ) {
+ return $e->getMessage();
+ }
+ }
+
+ /**
+ * @testdox A guest cannot hold a per-user coupon that the account owning the billing email already used.
+ */
+ public function test_hold_applied_coupons_counts_use_of_account_that_owns_guest_email(): void {
+ $user_id = $this->factory->user->create( array( 'user_email' => 'owner@example.com' ) );
+ WC_Helper_Coupon::create_coupon( 'hold_owner_use', array( 'usage_limit_per_user' => '1' ) );
+ add_post_meta( ( new WC_Coupon( 'hold_owner_use' ) )->get_id(), '_used_by', (string) $user_id );
+ wp_set_current_user( 0 );
+
+ $this->assertStringContainsString( 'You have used this coupon', (string) $this->try_hold_coupon( 'hold_owner_use', 'owner@example.com' ), 'A use stored under the owner ID, with no email record, is found by email' );
+ $this->assertNull( $this->try_hold_coupon( 'hold_owner_use', 'someone-else@example.com' ), 'An unrelated guest keeps their allowance' );
+ }
+
+ /**
+ * @testdox A logged in customer cannot take a second hold while their first hold is still active.
+ */
+ public function test_hold_applied_coupons_counts_own_active_hold(): void {
+ $user_id = $this->factory->user->create( array( 'user_email' => 'parallel@example.com' ) );
+ WC_Helper_Coupon::create_coupon( 'hold_parallel', array( 'usage_limit_per_user' => '1' ) );
+ wp_set_current_user( $user_id );
+
+ $this->assertNull( $this->try_hold_coupon( 'hold_parallel', 'parallel@example.com' ), 'The first checkout takes the hold' );
+ $this->assertStringContainsString( 'You have used this coupon', (string) $this->try_hold_coupon( 'hold_parallel', 'parallel@example.com' ), 'The second checkout sees the first hold' );
+ }
+
+ /**
+ * @testdox A per-user limit of two still lets the same customer take two holds.
+ */
+ public function test_hold_applied_coupons_allows_uses_up_to_limit(): void {
+ $user_id = $this->factory->user->create( array( 'user_email' => 'twice@example.com' ) );
+ WC_Helper_Coupon::create_coupon( 'hold_limit_two', array( 'usage_limit_per_user' => '2' ) );
+ wp_set_current_user( $user_id );
+
+ $this->assertNull( $this->try_hold_coupon( 'hold_limit_two', 'twice@example.com' ), 'First hold' );
+ $this->assertNull( $this->try_hold_coupon( 'hold_limit_two', 'twice@example.com' ), 'Second hold, still within the limit' );
+ $this->assertStringContainsString( 'You have used this coupon', (string) $this->try_hold_coupon( 'hold_limit_two', 'twice@example.com' ), 'Third hold is over the limit' );
+ }
+
+ /**
+ * @testdox A guest cannot hold a per-user coupon with an email whose owner used it, after the owner changes their account email.
+ */
+ public function test_hold_applied_coupons_counts_use_after_account_email_change(): void {
+ $user_id = $this->factory->user->create( array( 'user_email' => 'before@example.com' ) );
+ WC_Helper_Coupon::create_coupon( 'hold_email_change', array( 'usage_limit_per_user' => '1' ) );
+ ( new WC_Coupon( 'hold_email_change' ) )->increase_usage_count( (string) $user_id );
+ wp_update_user(
+ array(
+ 'ID' => $user_id,
+ 'user_email' => 'after@example.com',
+ )
+ );
+ wp_set_current_user( 0 );
+
+ $this->assertStringContainsString( 'You have used this coupon', (string) $this->try_hold_coupon( 'hold_email_change', 'before@example.com' ), 'The use is found through the email recorded with it' );
+ }
}
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php b/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php
index 807a4b14754..7e240c1507c 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php
@@ -3157,4 +3157,104 @@ class WC_Cart_Test extends \WC_Unit_Test_Case {
$this->assertFalse( $cart_item_key, 'Add to cart should reject a variation when an earlier variation matches.' );
$this->assertSame( 'The selected options belong to a different variation of this product.', wc_get_notices( 'error' )[0]['notice'] );
}
+
+ /**
+ * Run the classic checkout's per-user check for a guest and return the error notices it added.
+ *
+ * @param string $code Coupon code.
+ * @param string $billing_email Posted billing email.
+ * @return string[]
+ */
+ private function get_guest_coupon_check_errors( string $code, string $billing_email ): array {
+ wp_set_current_user( 0 );
+ WC()->cart->empty_cart();
+ WC()->cart->add_to_cart( WC_Helper_Product::create_simple_product()->get_id() );
+ WC()->cart->set_applied_coupons( array( $code ) );
+ wc_clear_notices();
+
+ WC()->cart->check_customer_coupons( array( 'billing_email' => $billing_email ) );
+
+ return wp_list_pluck( wc_get_notices( 'error' ), 'notice' );
+ }
+
+ /**
+ * @testdox The classic checkout refuses a guest a per-user coupon that the account owning the billing email already used.
+ */
+ public function test_check_customer_coupons_counts_use_of_account_that_owns_guest_email(): void {
+ $user_id = $this->factory->user->create( array( 'user_email' => 'cart-owner@example.com' ) );
+ WC_Helper_Coupon::create_coupon( 'cart_owner_use', array( 'usage_limit_per_user' => '1' ) );
+ add_post_meta( ( new WC_Coupon( 'cart_owner_use' ) )->get_id(), '_used_by', (string) $user_id );
+
+ $errors = $this->get_guest_coupon_check_errors( 'cart_owner_use', 'cart-owner@example.com' );
+
+ $this->assertStringContainsString( 'Usage limit for coupon', implode( ' ', $errors ), 'A use stored under the owner ID, with no email record, is found by email' );
+ $this->assertSame( array(), $this->get_guest_coupon_check_errors( 'cart_owner_use', 'cart-other@example.com' ), 'An unrelated guest keeps their allowance' );
+ }
+
+ /**
+ * @testdox The classic checkout keeps the usage count of a custom coupon data store that overrides get_usage_by_email().
+ */
+ public function test_check_customer_coupons_keeps_custom_store_email_usage_count(): void {
+ WC_Helper_Coupon::create_coupon( 'cart_custom_store', array( 'usage_limit_per_user' => '1' ) );
+ $store = new class() extends WC_Coupon_Data_Store_CPT {
+ /**
+ * Report one use for every email.
+ *
+ * @param WC_Coupon $coupon Coupon object.
+ * @param string $email Email address.
+ * @return int
+ */
+ public function get_usage_by_email( &$coupon, $email ) {
+ return 1;
+ }
+ };
+ $filter = fn() => $store;
+ add_filter( 'woocommerce_coupon_data_store', $filter );
+
+ $errors = $this->get_guest_coupon_check_errors( 'cart_custom_store', 'cart-custom@example.com' );
+ remove_filter( 'woocommerce_coupon_data_store', $filter );
+
+ $this->assertStringContainsString( 'Usage limit for coupon', implode( ' ', $errors ), 'The custom count is used' );
+ }
+
+ /**
+ * @testdox The classic checkout counts a guest's earlier use once when the coupon allows two uses per user.
+ */
+ public function test_check_customer_coupons_counts_guest_use_once(): void {
+ WC_Helper_Coupon::create_coupon( 'cart_two_uses', array( 'usage_limit_per_user' => '2' ) );
+ add_post_meta( ( new WC_Coupon( 'cart_two_uses' ) )->get_id(), '_used_by', 'cart-two@example.com' );
+
+ $this->assertSame( array(), $this->get_guest_coupon_check_errors( 'cart_two_uses', 'cart-two@example.com' ), 'One of two uses is left' );
+ }
+
+ /**
+ * @testdox The classic checkout tells a guest to try again later when the account owning the billing email holds the coupon.
+ */
+ public function test_check_customer_coupons_shows_try_again_message_for_owner_hold(): void {
+ $user_id = $this->factory->user->create( array( 'user_email' => 'cart-held@example.com' ) );
+ WC_Helper_Coupon::create_coupon( 'cart_owner_hold', array( 'usage_limit_per_user' => '1' ) );
+ $coupon = new WC_Coupon( 'cart_owner_hold' );
+ WC_Data_Store::load( 'coupon' )->check_and_hold_coupon_for_user( $coupon, array( $user_id ), $user_id );
+
+ $errors = $this->get_guest_coupon_check_errors( 'cart_owner_hold', 'cart-held@example.com' );
+
+ $this->assertStringContainsString( 'Please try again after some time', implode( ' ', $errors ) );
+ }
+
+ /**
+ * @testdox The classic checkout refuses a guest a per-user coupon with an email whose owner used it, after the owner changes their account email.
+ */
+ public function test_check_customer_coupons_counts_use_after_account_email_change(): void {
+ $user_id = $this->factory->user->create( array( 'user_email' => 'cart-before@example.com' ) );
+ WC_Helper_Coupon::create_coupon( 'cart_email_change', array( 'usage_limit_per_user' => '1' ) );
+ ( new WC_Coupon( 'cart_email_change' ) )->increase_usage_count( (string) $user_id );
+ wp_update_user(
+ array(
+ 'ID' => $user_id,
+ 'user_email' => 'cart-after@example.com',
+ )
+ );
+
+ $this->assertStringContainsString( 'Usage limit for coupon', implode( ' ', $this->get_guest_coupon_check_errors( 'cart_email_change', 'cart-before@example.com' ) ), 'The use is found through the email recorded with it' );
+ }
}
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-privacy-test.php b/plugins/woocommerce/tests/php/includes/class-wc-privacy-test.php
index 0ac50ca66fd..b95c2e3b01f 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-privacy-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-privacy-test.php
@@ -82,4 +82,46 @@ class WC_Privacy_Test extends WC_Unit_Test_Case {
$this->assertEquals( 1, $coupon->get_usage_count(), 'The count drops by one.' );
$this->assertContains( 'other@example.com', $coupon->get_used_by(), 'The other order keeps its row.' );
}
+
+ /**
+ * @testdox Erasing a customer removes their email records from coupons and keeps other people's.
+ *
+ * @return void
+ */
+ public function test_customer_data_eraser_removes_coupon_email_records(): void {
+ $customer_id = WC_Helper_Customer::create_customer( 'eraseme', 'password', 'eraseme@example.com' )->get_id();
+ $coupon_a = WC_Helper_Coupon::create_coupon( 'erase_records_a' )->get_id();
+ $coupon_b = WC_Helper_Coupon::create_coupon( 'erase_records_b' )->get_id();
+
+ add_post_meta( $coupon_a, '_used_by_email', $customer_id . ':eraseme@example.com' );
+ add_post_meta( $coupon_a, '_used_by_email', $customer_id . ':billing@example.com' );
+ add_post_meta( $coupon_a, '_used_by_email', '999:keep@example.com' );
+ add_post_meta( $coupon_a, '_used_by_email', $customer_id . '0:other@example.com' );
+ add_post_meta( $coupon_b, '_used_by_email', '888:eraseme@example.com' );
+ add_post_meta( $coupon_b, '_used_by_email', '888:noteraseme@example.com' );
+
+ $response = WC_Privacy_Erasers::customer_data_eraser( 'eraseme@example.com', 1 );
+
+ $this->assertTrue( $response['items_removed'], 'The eraser reports removed items.' );
+ $this->assertContains( 'Removed customer email from coupon usage records', $response['messages'] );
+ $this->assertSame( array( '999:keep@example.com', $customer_id . '0:other@example.com' ), get_post_meta( $coupon_a, '_used_by_email', false ), 'All records of the user and of the email are gone, and a user ID that starts with the same digits is kept.' );
+ $this->assertSame( array( '888:noteraseme@example.com' ), get_post_meta( $coupon_b, '_used_by_email', false ), 'A similar email is kept.' );
+ }
+
+ /**
+ * @testdox Erasing an email with no account removes the records that hold it.
+ *
+ * @return void
+ */
+ public function test_customer_data_eraser_removes_coupon_email_records_without_account(): void {
+ $coupon_id = WC_Helper_Coupon::create_coupon( 'erase_records_guest' )->get_id();
+ add_post_meta( $coupon_id, '_used_by_email', '777:guest.billing@example.com' );
+ add_post_meta( $coupon_id, '_used_by_email', '777:owner@example.com' );
+
+ $response = WC_Privacy_Erasers::customer_data_eraser( 'Guest.Billing@example.com', 1 );
+
+ $this->assertTrue( $response['items_removed'], 'The eraser reports removed items.' );
+ $this->assertContains( 'Removed customer email from coupon usage records', $response['messages'] );
+ $this->assertSame( array( '777:owner@example.com' ), get_post_meta( $coupon_id, '_used_by_email', false ) );
+ }
}
diff --git a/plugins/woocommerce/tests/php/includes/data-stores/class-wc-coupon-data-store-cpt-test.php b/plugins/woocommerce/tests/php/includes/data-stores/class-wc-coupon-data-store-cpt-test.php
index 80faa58a46d..c366890a642 100644
--- a/plugins/woocommerce/tests/php/includes/data-stores/class-wc-coupon-data-store-cpt-test.php
+++ b/plugins/woocommerce/tests/php/includes/data-stores/class-wc-coupon-data-store-cpt-test.php
@@ -422,4 +422,150 @@ class WC_Coupon_Data_Store_CPT_Test extends WC_Unit_Test_Case {
$this->assertSame( 'It\'s 12" off', get_post( $coupon->get_id() )->post_excerpt );
}
+
+ /**
+ * Create a published coupon with a per-user usage limit.
+ *
+ * @param string $code Coupon code.
+ * @param int $limit_per_user Usage limit per user, 0 for none.
+ * @return WC_Coupon
+ */
+ private function create_limited_coupon( string $code, int $limit_per_user = 1 ): WC_Coupon {
+ WC_Helper_Coupon::create_coupon( $code, array( 'usage_limit_per_user' => $limit_per_user ? (string) $limit_per_user : '' ) );
+ return new WC_Coupon( $code );
+ }
+
+ /**
+ * Get the `_used_by_email` rows of a coupon.
+ *
+ * @param WC_Coupon $coupon Coupon object.
+ * @return string[]
+ */
+ private function get_used_by_emails( WC_Coupon $coupon ): array {
+ $values = get_post_meta( $coupon->get_id(), '_used_by_email', false );
+ sort( $values );
+ return $values;
+ }
+
+ /**
+ * @testdox A use counted under a user ID records the account email and the order billing email.
+ */
+ public function test_increase_usage_count_records_account_and_billing_email_for_user(): void {
+ $user_id = $this->factory->user->create( array( 'user_email' => 'Account@Example.com' ) );
+ $coupon = $this->create_limited_coupon( 'record_user_emails' );
+ $order = WC_Helper_Order::create_order( $user_id );
+ $order->set_billing_email( 'billing@example.com' );
+ $order->save();
+
+ $coupon->increase_usage_count( (string) $user_id, $order );
+
+ $this->assertSame(
+ array( $user_id . ':account@example.com', $user_id . ':billing@example.com' ),
+ $this->get_used_by_emails( $coupon )
+ );
+ }
+
+ /**
+ * @testdox No email record is written for guest uses or for coupons without a per-user limit.
+ */
+ public function test_increase_usage_count_skips_email_record_for_guests_and_unlimited_coupons(): void {
+ $user_id = $this->factory->user->create( array( 'user_email' => 'unlimited@example.com' ) );
+ $unlimited = $this->create_limited_coupon( 'no_per_user_limit', 0 );
+ $limited = $this->create_limited_coupon( 'guest_use' );
+
+ $unlimited->increase_usage_count( (string) $user_id );
+ $limited->increase_usage_count( 'guest@example.com' );
+
+ $this->assertSame( array(), $this->get_used_by_emails( $unlimited ) );
+ $this->assertSame( array(), $this->get_used_by_emails( $limited ) );
+ }
+
+ /**
+ * @testdox Repeated uses by the same user do not duplicate their email records.
+ */
+ public function test_increase_usage_count_does_not_duplicate_email_records(): void {
+ $user_id = $this->factory->user->create( array( 'user_email' => 'repeat@example.com' ) );
+ $coupon = $this->create_limited_coupon( 'repeat_user', 3 );
+
+ $coupon->increase_usage_count( (string) $user_id );
+ $coupon->increase_usage_count( (string) $user_id );
+
+ $this->assertSame( array( $user_id . ':repeat@example.com' ), $this->get_used_by_emails( $coupon ) );
+ $this->assertCount( 2, ( new WC_Coupon( 'repeat_user' ) )->get_used_by() );
+ }
+
+ /**
+ * @testdox A user's email records are removed only when their last use of the coupon is removed.
+ */
+ public function test_decrease_usage_count_removes_email_records_after_last_use(): void {
+ $user_id = $this->factory->user->create( array( 'user_email' => 'release@example.com' ) );
+ $other_id = $this->factory->user->create( array( 'user_email' => 'other@example.com' ) );
+ $coupon = $this->create_limited_coupon( 'release_user', 3 );
+
+ $coupon->increase_usage_count( (string) $user_id );
+ $coupon->increase_usage_count( (string) $user_id );
+ $coupon->increase_usage_count( (string) $other_id );
+
+ $coupon->decrease_usage_count( (string) $user_id );
+ $this->assertContains( $user_id . ':release@example.com', $this->get_used_by_emails( $coupon ), 'One use remains, so the record stays' );
+
+ $coupon->decrease_usage_count( (string) $user_id );
+ $this->assertSame( array( $other_id . ':other@example.com' ), $this->get_used_by_emails( $coupon ) );
+ }
+
+ /**
+ * @testdox Recorded user IDs are added for matching emails on the same coupon only.
+ */
+ public function test_add_recorded_user_ids_to_aliases_maps_emails_to_users(): void {
+ $user_id = $this->factory->user->create( array( 'user_email' => 'mapped@example.com' ) );
+ $coupon = $this->create_limited_coupon( 'map_aliases' );
+ $other = $this->create_limited_coupon( 'map_aliases_other' );
+ $coupon->increase_usage_count( (string) $user_id );
+ wp_update_user(
+ array(
+ 'ID' => $user_id,
+ 'user_email' => 'renamed@example.com',
+ )
+ );
+
+ $data_store = WC_Data_Store::load( 'coupon' );
+
+ $this->assertEqualsCanonicalizing(
+ array( 'Mapped@Example.com', $user_id ),
+ $data_store->add_recorded_user_ids_to_aliases( $coupon, array( 'Mapped@Example.com' ) )
+ );
+ $this->assertSame( array( 'mapped@example.com' ), $data_store->add_recorded_user_ids_to_aliases( $other, array( 'mapped@example.com' ) ) );
+ $this->assertSame( array( 'unrelated@example.com' ), $data_store->add_recorded_user_ids_to_aliases( $coupon, array( 'unrelated@example.com' ) ) );
+ }
+
+ /**
+ * @testdox Usage by aliases counts each use once, including uses found through a recorded email and current holds.
+ */
+ public function test_get_usage_by_aliases_counts_each_use_once(): void {
+ $user_id = $this->factory->user->create( array( 'user_email' => 'count@example.com' ) );
+ $coupon = $this->create_limited_coupon( 'count_aliases', 5 );
+ $coupon->increase_usage_count( (string) $user_id );
+ $coupon->increase_usage_count( 'count@example.com' );
+
+ $data_store = WC_Data_Store::load( 'coupon' );
+
+ $this->assertSame( 2, $data_store->get_usage_by_aliases( $coupon, array( 'count@example.com' ) ) );
+ $this->assertSame( 2, $data_store->get_usage_by_aliases( $coupon, array( 'count@example.com', $user_id, (string) $user_id ) ) );
+
+ $data_store->check_and_hold_coupon_for_user( $coupon, array( 'count@example.com' ), 'count@example.com' );
+ $this->assertSame( 3, $data_store->get_usage_by_aliases( $coupon, array( 'count@example.com' ) ) );
+ }
+
+ /**
+ * @testdox Email records are not exposed as coupon meta data.
+ */
+ public function test_used_by_email_is_internal_meta(): void {
+ $user_id = $this->factory->user->create( array( 'user_email' => 'internal@example.com' ) );
+ $coupon = $this->create_limited_coupon( 'internal_meta' );
+ $coupon->increase_usage_count( (string) $user_id );
+
+ $keys = wp_list_pluck( ( new WC_Coupon( 'internal_meta' ) )->get_meta_data(), 'key' );
+
+ $this->assertNotContains( '_used_by_email', $keys );
+ }
}
diff --git a/plugins/woocommerce/tests/php/includes/wc-order-functions-test.php b/plugins/woocommerce/tests/php/includes/wc-order-functions-test.php
index 281da934c66..de9b8ca18fe 100644
--- a/plugins/woocommerce/tests/php/includes/wc-order-functions-test.php
+++ b/plugins/woocommerce/tests/php/includes/wc-order-functions-test.php
@@ -1317,4 +1317,48 @@ class WC_Order_Functions_Test extends \WC_Unit_Test_Case {
$downloads = WC_Data_Store::load( 'customer-download' )->get_downloads( array( 'order_id' => $order->get_id() ) );
return array_map( fn( $download ) => (int) $download->get_product_id(), $downloads );
}
+
+ /**
+ * @testdox Counting a customer's order records its emails on a per-user coupon, and cancelling it removes them with the use.
+ */
+ public function test_customer_order_records_and_releases_used_by_emails() {
+ $customer_id = $this->factory->user->create( array( 'user_email' => 'lifecycle@example.com' ) );
+ WC_Helper_Coupon::create_coupon( 'lifecycle_emails', array( 'usage_limit_per_user' => '1' ) );
+
+ $order = $this->create_counted_customer_order( $customer_id, 'lifecycle_emails' );
+
+ $coupon_id = ( new WC_Coupon( 'lifecycle_emails' ) )->get_id();
+ $this->assertContains( $customer_id . ':lifecycle@example.com', get_post_meta( $coupon_id, '_used_by_email', false ) );
+ $this->assertContains( $customer_id . ':' . strtolower( $order->get_billing_email() ), get_post_meta( $coupon_id, '_used_by_email', false ) );
+
+ wc_get_order( $order->get_id() )->update_status( OrderStatus::CANCELLED );
+
+ $coupon = new WC_Coupon( 'lifecycle_emails' );
+ $this->assertSame( array(), array_values( $coupon->get_used_by() ) );
+ $this->assertEquals( 0, $coupon->get_usage_count() );
+ $this->assertSame( array(), get_post_meta( $coupon_id, '_used_by_email', false ) );
+ }
+
+ /**
+ * @testdox Applying a per-user coupon to a customer order that already counts its coupons records the order's billing email.
+ */
+ public function test_apply_coupon_to_counted_order_records_billing_email() {
+ $customer_id = $this->factory->user->create( array( 'user_email' => 'apply-account@example.com' ) );
+ WC_Helper_Coupon::create_coupon( 'apply_counted_emails', array( 'usage_limit_per_user' => '1' ) );
+
+ $order = WC_Helper_Order::create_order( $customer_id );
+ $order->set_billing_email( 'apply-billing@example.com' );
+ $order->set_status( OrderStatus::PROCESSING );
+ $order->save();
+ $order->get_data_store()->set_recorded_coupon_usage_counts( $order, true );
+
+ wc_get_order( $order->get_id() )->apply_coupon( 'apply_counted_emails' );
+
+ $coupon = new WC_Coupon( 'apply_counted_emails' );
+ $records = get_post_meta( $coupon->get_id(), '_used_by_email', false );
+ $this->assertContains( $customer_id . ':apply-account@example.com', $records );
+ $this->assertContains( $customer_id . ':apply-billing@example.com', $records );
+ $this->assertEquals( 1, $coupon->get_usage_count(), 'The use is counted once.' );
+ $this->assertSame( array( (string) $customer_id ), array_values( $coupon->get_used_by() ) );
+ }
}
diff --git a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/OrderControllerTests.php b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/OrderControllerTests.php
index 8772efcba45..c05a6466b87 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/OrderControllerTests.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/OrderControllerTests.php
@@ -997,4 +997,111 @@ class OrderControllerTests extends \WC_Unit_Test_Case {
$order->{"set_shipping_$key"}( $value );
}
}
+
+ /**
+ * Build a saved order for a customer and billing email that carries a coupon, without recording its usage.
+ *
+ * @param int $customer_id Customer ID, 0 for a guest.
+ * @param string $billing_email Billing email.
+ * @param string $code Coupon code.
+ * @return \WC_Order
+ */
+ private function create_order_with_coupon( int $customer_id, string $billing_email, string $code ): \WC_Order {
+ $order = WC_Helper_Order::create_order( $customer_id );
+ $order->set_billing_email( $billing_email );
+ $this->set_shipping_address( $order );
+ $item = new \WC_Order_Item_Coupon();
+ $item->set_code( $code );
+ $order->add_item( $item );
+ $order->save();
+ return $order;
+ }
+
+ /**
+ * Run payment validation and return the coupon error message, or null when the coupon was kept.
+ *
+ * @param \WC_Order $order Order to validate.
+ * @return string|null
+ */
+ private function get_coupon_error( \WC_Order $order ): ?string {
+ try {
+ $this->sut->validate_order_before_payment( $order );
+ return null;
+ } catch ( RouteException $e ) {
+ return $e->getMessage();
+ }
+ }
+
+ /**
+ * @testdox A guest is refused a per-user coupon with the email of an account that used it, after that account changes its email.
+ */
+ public function test_validate_coupon_usage_limit_after_account_email_change(): void {
+ $user_id = $this->factory->user->create( array( 'user_email' => 'vip-store@example.com' ) );
+ $coupon = CouponHelper::create_coupon(
+ 'vip-store',
+ 'publish',
+ array(
+ 'customer_email' => array( 'vip-store@example.com' ),
+ 'usage_limit_per_user' => 1,
+ )
+ );
+
+ $first_order = $this->create_order_with_coupon( $user_id, 'invoices@example.com', 'vip-store' );
+ $coupon->increase_usage_count( (string) $user_id, $first_order );
+
+ wp_update_user(
+ array(
+ 'ID' => $user_id,
+ 'user_email' => 'changed-store@example.com',
+ )
+ );
+
+ $error = $this->get_coupon_error( $this->create_order_with_coupon( 0, 'vip-store@example.com', 'vip-store' ) );
+
+ $this->assertStringContainsString( 'Usage limit for coupon', (string) $error );
+ }
+
+ /**
+ * @testdox A second account is refused a per-user coupon billed to an email already used, after the first customer edits their saved billing email.
+ */
+ public function test_validate_coupon_usage_limit_after_saved_billing_email_change(): void {
+ $first_id = $this->factory->user->create( array( 'user_email' => 'first-store@example.com' ) );
+ $second_id = $this->factory->user->create( array( 'user_email' => 'second-store@example.com' ) );
+ $coupon = CouponHelper::create_coupon( 'billing-store', 'publish', array( 'usage_limit_per_user' => 1 ) );
+
+ $first_order = $this->create_order_with_coupon( $first_id, 'shared-store@example.com', 'billing-store' );
+ $coupon->increase_usage_count( (string) $first_id, $first_order );
+ update_user_meta( $first_id, 'billing_email', 'moved-store@example.com' );
+
+ $second_error = $this->get_coupon_error( $this->create_order_with_coupon( $second_id, 'shared-store@example.com', 'billing-store' ) );
+ $guest_error = $this->get_coupon_error( $this->create_order_with_coupon( 0, 'shared-store@example.com', 'billing-store' ) );
+
+ $this->assertStringContainsString( 'Usage limit for coupon', (string) $second_error );
+ $this->assertStringContainsString( 'Usage limit for coupon', (string) $guest_error );
+ }
+
+ /**
+ * @testdox A per-user limit of two still accepts a second use by a guest with the email of an account that used it once.
+ */
+ public function test_validate_coupon_usage_limit_allows_uses_up_to_limit(): void {
+ $user_id = $this->factory->user->create( array( 'user_email' => 'two-store@example.com' ) );
+ $coupon = CouponHelper::create_coupon( 'two-store', 'publish', array( 'usage_limit_per_user' => 2 ) );
+ $coupon->increase_usage_count( (string) $user_id, $this->create_order_with_coupon( $user_id, 'two-store@example.com', 'two-store' ) );
+
+ $this->assertNull( $this->get_coupon_error( $this->create_order_with_coupon( 0, 'two-store@example.com', 'two-store' ) ) );
+ }
+
+ /**
+ * @testdox A second account is refused a per-user coupon when it bills to the account email of a customer who used it.
+ */
+ public function test_validate_coupon_usage_limit_second_account_billing_to_used_account_email(): void {
+ $first_id = $this->factory->user->create( array( 'user_email' => 'owner-store@example.com' ) );
+ $second_id = $this->factory->user->create( array( 'user_email' => 'other-store@example.com' ) );
+ $coupon = CouponHelper::create_coupon( 'owner-store', 'publish', array( 'usage_limit_per_user' => 1 ) );
+ $coupon->increase_usage_count( (string) $first_id, $this->create_order_with_coupon( $first_id, 'receipts-store@example.com', 'owner-store' ) );
+
+ $error = $this->get_coupon_error( $this->create_order_with_coupon( $second_id, 'owner-store@example.com', 'owner-store' ) );
+
+ $this->assertStringContainsString( 'Usage limit for coupon', (string) $error );
+ }
}