Merge pull request #16440 from woocommerce/fix/16422
Adjust coupon user limit logic
This commit is contained in:
commit
28c5af4922
|
@ -459,9 +459,6 @@ class WC_Cart extends WC_Legacy_Cart {
|
|||
if ( ! $coupon->is_valid() ) {
|
||||
$coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_INVALID_REMOVED );
|
||||
$this->remove_coupon( $code );
|
||||
|
||||
// Flag totals for refresh.
|
||||
WC()->session->set( 'refresh_totals', true );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1344,54 +1341,55 @@ class WC_Cart extends WC_Legacy_Cart {
|
|||
|
||||
if ( $coupon->is_valid() ) {
|
||||
|
||||
// Get user and posted emails to compare.
|
||||
$current_user = wp_get_current_user();
|
||||
$check_emails = array_unique( array_filter( array_map( 'strtolower', array_map( 'sanitize_email', array(
|
||||
$posted['billing_email'],
|
||||
$current_user->user_email,
|
||||
) ) ) ) );
|
||||
|
||||
// Limit to defined email addresses.
|
||||
if ( is_array( $coupon->get_email_restrictions() ) && count( $coupon->get_email_restrictions() ) > 0 ) {
|
||||
$check_emails = array();
|
||||
if ( is_user_logged_in() ) {
|
||||
$current_user = wp_get_current_user();
|
||||
$check_emails[] = $current_user->user_email;
|
||||
}
|
||||
$check_emails[] = $posted['billing_email'];
|
||||
$check_emails = array_map( 'sanitize_email', array_map( 'strtolower', $check_emails ) );
|
||||
$restrictions = $coupon->get_email_restrictions();
|
||||
|
||||
if ( 0 === count( array_intersect( $check_emails, $coupon->get_email_restrictions() ) ) ) {
|
||||
$coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_NOT_YOURS_REMOVED );
|
||||
$this->remove_coupon( $code );
|
||||
|
||||
// Flag totals for refresh.
|
||||
WC()->session->set( 'refresh_totals', true );
|
||||
}
|
||||
if ( is_array( $restrictions ) && 0 < count( $restrictions ) && 0 === count( array_intersect( $check_emails, $restrictions ) ) ) {
|
||||
$coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_NOT_YOURS_REMOVED );
|
||||
$this->remove_coupon( $code );
|
||||
}
|
||||
|
||||
// Usage limits per user - check against billing and user email and user ID.
|
||||
if ( $coupon->get_usage_limit_per_user() > 0 ) {
|
||||
$check_emails = array();
|
||||
$used_by = $coupon->get_used_by();
|
||||
$limit_per_user = $coupon->get_usage_limit_per_user();
|
||||
|
||||
if ( is_user_logged_in() ) {
|
||||
$current_user = wp_get_current_user();
|
||||
$check_emails[] = sanitize_email( $current_user->user_email );
|
||||
$usage_count = count( array_keys( $used_by, get_current_user_id(), true ) );
|
||||
} else {
|
||||
$check_emails[] = sanitize_email( $posted['billing_email'] );
|
||||
$user = get_user_by( 'email', $posted['billing_email'] );
|
||||
if ( $user ) {
|
||||
$usage_count = count( array_keys( $used_by, $user->ID, true ) );
|
||||
} else {
|
||||
$usage_count = 0;
|
||||
}
|
||||
if ( 0 < $limit_per_user ) {
|
||||
$used_by = $coupon->get_used_by();
|
||||
$usage_count = 0;
|
||||
$user_id_matches = array( get_current_user_id() );
|
||||
|
||||
// Check usage against emails.
|
||||
foreach ( $check_emails as $check_email ) {
|
||||
$usage_count += count( array_keys( $used_by, $check_email, true ) );
|
||||
$user = get_user_by( 'email', $check_email );
|
||||
$user_id_matches[] = $user ? $user->ID : 0;
|
||||
}
|
||||
|
||||
foreach ( $check_emails as $check_email ) {
|
||||
$usage_count = $usage_count + count( array_keys( $used_by, $check_email, true ) );
|
||||
// Check against billing emails of existing users.
|
||||
$users_query = new WP_User_Query( array(
|
||||
'fields' => 'ID',
|
||||
'meta_query' => array(
|
||||
'key' => '_billing_email',
|
||||
'value' => $check_emails,
|
||||
'compare' => 'IN',
|
||||
),
|
||||
) );
|
||||
|
||||
$user_id_matches = array_unique( array_filter( array_merge( $user_id_matches, $users_query->get_results() ) ) );
|
||||
|
||||
foreach ( $user_id_matches as $user_id ) {
|
||||
$usage_count += count( array_keys( $used_by, $user_id ) );
|
||||
}
|
||||
|
||||
if ( $usage_count >= $coupon->get_usage_limit_per_user() ) {
|
||||
$coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_USAGE_LIMIT_REACHED );
|
||||
$this->remove_coupon( $code );
|
||||
|
||||
// Flag totals for refresh.
|
||||
WC()->session->set( 'refresh_totals', true );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1589,6 +1587,7 @@ class WC_Cart extends WC_Legacy_Cart {
|
|||
}
|
||||
|
||||
WC()->session->set( 'applied_coupons', $this->applied_coupons );
|
||||
WC()->session->set( 'refresh_totals', true );
|
||||
|
||||
do_action( 'woocommerce_removed_coupon', $coupon_code );
|
||||
|
||||
|
|
|
@ -582,7 +582,7 @@ class WC_Discounts {
|
|||
$user_id = get_current_user_id();
|
||||
}
|
||||
|
||||
if ( $coupon->get_usage_limit_per_user() > 0 && is_user_logged_in() && $coupon->get_id() && $coupon->get_data_store() ) {
|
||||
if ( $coupon && $user_id && $coupon->get_usage_limit_per_user() > 0 && $coupon->get_id() && $coupon->get_data_store() ) {
|
||||
$date_store = $coupon->get_data_store();
|
||||
$usage_count = $date_store->get_usage_by_user_id( $coupon, $user_id );
|
||||
if ( $usage_count >= $coupon->get_usage_limit_per_user() ) {
|
||||
|
|
Loading…
Reference in New Issue