From a6a65a2a9df80fcb62541545a26b79c4cd9e032b Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Thu, 24 Apr 2014 15:26:34 +0100 Subject: [PATCH] typecast $used_by to array in case its not set. Closes #5350 --- includes/class-wc-cart.php | 6 +++--- includes/class-wc-coupon.php | 8 +++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/includes/class-wc-cart.php b/includes/class-wc-cart.php index ccf97adee27..656f48b32e4 100644 --- a/includes/class-wc-cart.php +++ b/includes/class-wc-cart.php @@ -1487,7 +1487,7 @@ 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 = get_post_meta( $this->id, '_used_by' ); + $used_by = (array) get_post_meta( $this->id, '_used_by' ); if ( is_user_logged_in() ) { $current_user = wp_get_current_user(); @@ -1495,11 +1495,11 @@ class WC_Cart { } $check_emails[] = $posted['billing_email']; $check_emails = array_map( 'sanitize_email', array_map( 'strtolower', $check_emails ) ); - $usage_count = sizeof( array_keys( $used_by, get_current_user_id() ) ); - foreach ( $check_emails as $check_email ) + foreach ( $check_emails as $check_email ) { $usage_count = $usage_count + sizeof( array_keys( $used_by, $check_email ) ); + } if ( $usage_count >= $coupon->usage_limit_per_user ) { $coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_USAGE_LIMIT_REACHED ); diff --git a/includes/class-wc-coupon.php b/includes/class-wc-coupon.php index cf5b042b82f..39bb6214bf9 100644 --- a/includes/class-wc-coupon.php +++ b/includes/class-wc-coupon.php @@ -229,8 +229,9 @@ class WC_Coupon { $this->usage_count++; update_post_meta( $this->id, 'usage_count', $this->usage_count ); - if ( $used_by ) + if ( $used_by ) { add_post_meta( $this->id, '_used_by', strtolower( $used_by ) ); + } } @@ -249,8 +250,9 @@ class WC_Coupon { // Delete 1 used by meta $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = '_used_by' AND meta_value = %s AND post_id = %d LIMIT 1;", $used_by, $this->id ) ); - if ( $meta_id ) + if ( $meta_id ) { delete_metadata_by_mid( 'post', $meta_id ); + } } /** @@ -290,7 +292,7 @@ class WC_Coupon { // Per user usage limit - check here if user is logged in (against user IDs) // Checked again for emails later on in WC_Cart::check_customer_coupons() if ( $this->usage_limit_per_user > 0 && is_user_logged_in() ) { - $used_by = get_post_meta( $this->id, '_used_by' ); + $used_by = (array) get_post_meta( $this->id, '_used_by' ); $usage_count = sizeof( array_keys( $used_by, get_current_user_id() ) ); if ( $usage_count >= $this->usage_limit_per_user ) {