Skip to content

Commit 4e36b83

Browse files
authored
Merge pull request #3175 from dparker1005/one-use-per-user-codes
Adding "one use per user" discount code setting
2 parents 7a7ec7c + 6a708d2 commit 4e36b83

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

adminpages/discountcodes.php

+21-7
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
$expires_day = intval($_POST['expires_day']);
6969
$expires_year = intval($_POST['expires_year']);
7070
$uses = intval($_POST['uses']);
71+
$one_use_per_user = ! empty( $_POST['one_use_per_user'] ) ? 1 : 0;
7172

7273
//fix up dates
7374
$starts = date("Y-m-d", strtotime($starts_month . "/" . $starts_day . "/" . $starts_year, $now ));
@@ -81,13 +82,15 @@
8182
'code' => $code,
8283
'starts' => $starts,
8384
'expires' => $expires,
84-
'uses' => $uses
85+
'uses' => $uses,
86+
'one_use_per_user' => $one_use_per_user
8587
),
8688
array(
8789
'%d',
8890
'%s',
8991
'%s',
9092
'%s',
93+
'%d',
9194
'%d'
9295
)
9396
);
@@ -446,7 +449,7 @@
446449

447450
<tr>
448451
<th scope="row" valign="top"><label for="code"><?php esc_html_e('Code', 'paid-memberships-pro' );?></label></th>
449-
<td><input name="code" type="text" size="20" value="<?php echo esc_attr( $code->code ); ?>" /></td>
452+
<td><input name="code" id="code" type="text" size="20" value="<?php echo esc_attr( $code->code ); ?>" /></td>
450453
</tr>
451454

452455
<?php
@@ -485,7 +488,7 @@
485488
<tr>
486489
<th scope="row" valign="top"><label for="starts"><?php esc_html_e('Start Date', 'paid-memberships-pro' );?></label></th>
487490
<td>
488-
<select name="starts_month">
491+
<select name="starts_month" id="starts">
489492
<?php
490493
for($i = 1; $i < 13; $i++)
491494
{
@@ -503,7 +506,7 @@
503506
<tr>
504507
<th scope="row" valign="top"><label for="expires"><?php esc_html_e('Expiration Date', 'paid-memberships-pro' );?></label></th>
505508
<td>
506-
<select name="expires_month">
509+
<select name="expires_month" id="expires">
507510
<?php
508511
for($i = 1; $i < 13; $i++)
509512
{
@@ -519,10 +522,21 @@
519522
</tr>
520523

521524
<tr>
522-
<th scope="row" valign="top"><label for="uses"><?php esc_html_e('Uses', 'paid-memberships-pro' );?></label></th>
525+
<th scope="row" valign="top"><label for="uses"><?php esc_html_e( 'Limit Total Uses', 'paid-memberships-pro' );?></label></th>
526+
<td>
527+
<input name="uses" id="uses" type="text" size="10" value="<?php if ( ! empty( $code->uses ) ) echo esc_attr( $code->uses ); ?>" />
528+
<p class="description">
529+
<?php esc_html_e( 'Define the maximum number of times this discount code can be used across all users.', 'paid-memberships-pro' ); ?>
530+
<?php esc_html_e('Leave blank for unlimited uses.', 'paid-memberships-pro' ); ?>
531+
</p>
532+
</td>
533+
</tr>
534+
535+
<tr>
536+
<th scope="row" valign="top"><label for="one_use_per_user"><?php esc_html_e( 'Limit Per User', 'paid-memberships-pro' );?></label></th>
523537
<td>
524-
<input name="uses" type="text" size="10" value="<?php if ( ! empty( $code->uses ) ) echo esc_attr( $code->uses ); ?>" />
525-
<p class="description"><?php esc_html_e('Leave blank for unlimited uses.', 'paid-memberships-pro' );?></p>
538+
<input name="one_use_per_user" id="one_use_per_user" type="checkbox" value="1" <?php if ( ! empty( $code->one_use_per_user ) ) checked( $code->one_use_per_user, 1 ); ?> />
539+
<label for="one_use_per_user"><?php esc_html_e('Restrict this discount code to a single use per unique user.', 'paid-memberships-pro' );?></label>
526540
</td>
527541
</tr>
528542

includes/functions.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -1890,7 +1890,7 @@ function pmpro_getDiscountCode( $seed = null ) {
18901890
* Is a discount code valid - $level_id could be a scalar or an array (or unset)
18911891
*/
18921892
function pmpro_checkDiscountCode( $code, $level_id = null, $return_errors = false ) {
1893-
global $wpdb;
1893+
global $wpdb, $current_user;
18941894

18951895
$error = false;
18961896
$dbcode = false;
@@ -1942,6 +1942,16 @@ function pmpro_checkDiscountCode( $code, $level_id = null, $return_errors = fals
19421942
}
19431943
}
19441944

1945+
// check if this code is limited to one use per user
1946+
if ( ! $error ) {
1947+
if ( ! empty( $dbcode->one_use_per_user ) && ! empty( $current_user->ID ) ) {
1948+
$used = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->pmpro_discount_codes_uses WHERE code_id = '" . esc_sql( $dbcode->id ) . "' AND user_id = '" . esc_sql( $current_user->ID ) . "'" );
1949+
if ( $used > 0 ) {
1950+
$error = __( 'You have already used the discount code provided.', 'paid-memberships-pro' );
1951+
}
1952+
}
1953+
}
1954+
19451955
// if a level was passed check if this code applies
19461956
if ( ! $error ) {
19471957
$pmpro_check_discount_code_levels = apply_filters( 'pmpro_check_discount_code_levels', true, $dbcode->id );

includes/upgradecheck.php

+10
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,15 @@ function pmpro_checkForUpgrades() {
392392
pmpro_upgrade_3_2();
393393
update_option( 'pmpro_db_version', '3.2' );
394394
}
395+
396+
/**
397+
* Version 3.4
398+
* Adding `one_use_per_user` column to discount codes.
399+
*/
400+
if ( $pmpro_db_version < 3.4 ) {
401+
pmpro_db_delta();
402+
update_option( 'pmpro_db_version', '3.4' );
403+
}
395404
}
396405

397406
function pmpro_db_delta() {
@@ -567,6 +576,7 @@ function pmpro_db_delta() {
567576
`starts` date NOT NULL,
568577
`expires` date NOT NULL,
569578
`uses` int(11) NOT NULL,
579+
`one_use_per_user` tinyint(4) NOT NULL DEFAULT '0',
570580
PRIMARY KEY (`id`),
571581
UNIQUE KEY `code` (`code`),
572582
KEY `starts` (`starts`),

0 commit comments

Comments
 (0)