[2.1] Fix coupon limit checks and enhance to check ID by provided email (if logged out)

Fixes #5535
This commit is contained in:
Mike Jolley 2014-05-22 16:23:01 +01:00
parent 1d783ae36e
commit a109f47f37
1 changed files with 15 additions and 7 deletions

View File

@ -1476,6 +1476,7 @@ class WC_Cart {
// Limit to defined email addresses
if ( is_array( $coupon->customer_email ) && sizeof( $coupon->customer_email ) > 0 ) {
$check_emails = array();
$coupon->customer_email = array_map( 'sanitize_email', $coupon->customer_email );
if ( is_user_logged_in() ) {
@ -1498,15 +1499,22 @@ class WC_Cart {
// Usage limits per user - check against billing and user email and user ID
if ( $coupon->usage_limit_per_user > 0 ) {
$used_by = (array) get_post_meta( $this->id, '_used_by' );
$check_emails = array();
$used_by = array_filter( (array) get_post_meta( $coupon->id, '_used_by' ) );
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 ) );
$check_emails[] = sanitize_email( $current_user->user_email );
$usage_count = sizeof( array_keys( $used_by, get_current_user_id() ) );
} else {
$check_emails[] = sanitize_email( $posted['billing_email'] );
$user = get_user_by( 'email', $posted['billing_email'] );
if ( $user ) {
$usage_count = sizeof( array_keys( $used_by, $user->ID ) );
} else {
$usage_count = 0;
}
}
foreach ( $check_emails as $check_email ) {
$usage_count = $usage_count + sizeof( array_keys( $used_by, $check_email ) );