WC_Cart::check_customer_coupons PHP Method

check_customer_coupons() public method

Checks two types of coupons: 1. Where a list of customer emails are set (limits coupon usage to those defined). 2. Where a usage_limit_per_user is set (limits coupon usage to a number based on user ID and email).
public check_customer_coupons ( array $posted )
$posted array
    public function check_customer_coupons($posted)
    {
        if (!empty($this->applied_coupons)) {
            foreach ($this->applied_coupons as $code) {
                $coupon = new WC_Coupon($code);
                if ($coupon->is_valid()) {
                    // Limit to defined email addresses
                    if (is_array($coupon->get_email_restrictions()) && sizeof($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));
                        if (0 == sizeof(array_intersect($check_emails, $coupon->get_email_restrictions()))) {
                            $coupon->add_coupon_message(WC_Coupon::E_WC_COUPON_NOT_YOURS_REMOVED);
                            // Remove the coupon
                            $this->remove_coupon($code);
                            // Flag totals for refresh
                            WC()->session->set('refresh_totals', true);
                        }
                    }
                    // 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();
                        if (is_user_logged_in()) {
                            $current_user = wp_get_current_user();
                            $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));
                        }
                        if ($usage_count >= $coupon->get_usage_limit_per_user()) {
                            $coupon->add_coupon_message(WC_Coupon::E_WC_COUPON_USAGE_LIMIT_REACHED);
                            // Remove the coupon
                            $this->remove_coupon($code);
                            // Flag totals for refresh
                            WC()->session->set('refresh_totals', true);
                        }
                    }
                }
            }
        }
    }
WC_Cart