Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding "one use per user" discount code setting #3175

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions adminpages/discountcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
$expires_day = intval($_POST['expires_day']);
$expires_year = intval($_POST['expires_year']);
$uses = intval($_POST['uses']);
$one_use_per_user = ! empty( $_POST['one_use_per_user'] ) ? 1 : 0;

//fix up dates
$starts = date("Y-m-d", strtotime($starts_month . "/" . $starts_day . "/" . $starts_year, $now ));
Expand All @@ -81,13 +82,15 @@
'code' => $code,
'starts' => $starts,
'expires' => $expires,
'uses' => $uses
'uses' => $uses,
'one_use_per_user' => $one_use_per_user
),
array(
'%d',
'%s',
'%s',
'%s',
'%d',
'%d'
)
);
Expand Down Expand Up @@ -446,7 +449,7 @@

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

<?php
Expand Down Expand Up @@ -485,7 +488,7 @@
<tr>
<th scope="row" valign="top"><label for="starts"><?php esc_html_e('Start Date', 'paid-memberships-pro' );?></label></th>
<td>
<select name="starts_month">
<select name="starts_month" id="starts">
<?php
for($i = 1; $i < 13; $i++)
{
Expand All @@ -503,7 +506,7 @@
<tr>
<th scope="row" valign="top"><label for="expires"><?php esc_html_e('Expiration Date', 'paid-memberships-pro' );?></label></th>
<td>
<select name="expires_month">
<select name="expires_month" id="expires">
<?php
for($i = 1; $i < 13; $i++)
{
Expand All @@ -519,10 +522,21 @@
</tr>

<tr>
<th scope="row" valign="top"><label for="uses"><?php esc_html_e('Uses', 'paid-memberships-pro' );?></label></th>
<th scope="row" valign="top"><label for="uses"><?php esc_html_e( 'Limit Total Uses', 'paid-memberships-pro' );?></label></th>
<td>
<input name="uses" id="uses" type="text" size="10" value="<?php if ( ! empty( $code->uses ) ) echo esc_attr( $code->uses ); ?>" />
<p class="description">
<?php esc_html_e( 'Define the maximum number of times this discount code can be used across all users.', 'paid-memberships-pro' ); ?>
<?php esc_html_e('Leave blank for unlimited uses.', 'paid-memberships-pro' ); ?>
</p>
</td>
</tr>

<tr>
<th scope="row" valign="top"><label for="one_use_per_user"><?php esc_html_e( 'Limit Per User', 'paid-memberships-pro' );?></label></th>
<td>
<input name="uses" type="text" size="10" value="<?php if ( ! empty( $code->uses ) ) echo esc_attr( $code->uses ); ?>" />
<p class="description"><?php esc_html_e('Leave blank for unlimited uses.', 'paid-memberships-pro' );?></p>
<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 ); ?> />
<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>
</td>
</tr>

Expand Down
12 changes: 11 additions & 1 deletion includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1890,7 +1890,7 @@ function pmpro_getDiscountCode( $seed = null ) {
* Is a discount code valid - $level_id could be a scalar or an array (or unset)
*/
function pmpro_checkDiscountCode( $code, $level_id = null, $return_errors = false ) {
global $wpdb;
global $wpdb, $current_user;

$error = false;
$dbcode = false;
Expand Down Expand Up @@ -1942,6 +1942,16 @@ function pmpro_checkDiscountCode( $code, $level_id = null, $return_errors = fals
}
}

// check if this code is limited to one use per user
if ( ! $error ) {
if ( ! empty( $dbcode->one_use_per_user ) && ! empty( $current_user->ID ) ) {
$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 ) . "'" );
if ( $used > 0 ) {
$error = __( 'You have already used the discount code provided.', 'paid-memberships-pro' );
}
}
}

// if a level was passed check if this code applies
if ( ! $error ) {
$pmpro_check_discount_code_levels = apply_filters( 'pmpro_check_discount_code_levels', true, $dbcode->id );
Expand Down
10 changes: 10 additions & 0 deletions includes/upgradecheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,15 @@ function pmpro_checkForUpgrades() {
pmpro_upgrade_3_2();
update_option( 'pmpro_db_version', '3.2' );
}

/**
* Version 3.4
* Adding `one_use_per_user` column to discount codes.
*/
if ( $pmpro_db_version < 3.4 ) {
pmpro_db_delta();
update_option( 'pmpro_db_version', '3.4' );
}
}

function pmpro_db_delta() {
Expand Down Expand Up @@ -567,6 +576,7 @@ function pmpro_db_delta() {
`starts` date NOT NULL,
`expires` date NOT NULL,
`uses` int(11) NOT NULL,
`one_use_per_user` tinyint(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `code` (`code`),
KEY `starts` (`starts`),
Expand Down