Get records of all users who have used the current coupon.
public get_used_by ( string $context = 'view' ) : array | ||
$context | string | |
return | array |
/** * Prepare a single coupon output for response. * * @param WP_Post $post Post object. * @param WP_REST_Request $request Request object. * @return WP_REST_Response $data */ public function prepare_item_for_response($post, $request) { global $wpdb; // Get the coupon code. $code = $wpdb->get_var($wpdb->prepare("SELECT post_title FROM {$wpdb->posts} WHERE id = %s AND post_type = 'shop_coupon' AND post_status = 'publish'", $post->ID)); $coupon = new WC_Coupon($code); $data = array('id' => $coupon->id, 'code' => $coupon->code, 'date_created' => wc_rest_prepare_date_response($post->post_date_gmt), 'date_modified' => wc_rest_prepare_date_response($post->post_modified_gmt), 'discount_type' => $coupon->type, 'description' => $post->post_excerpt, 'amount' => wc_format_decimal($coupon->coupon_amount, 2), 'expiry_date' => !empty($coupon->expiry_date) ? wc_rest_prepare_date_response($coupon->expiry_date) : null, 'usage_count' => (int) $coupon->usage_count, 'individual_use' => 'yes' === $coupon->individual_use, 'product_ids' => array_map('absint', (array) $coupon->product_ids), 'exclude_product_ids' => array_map('absint', (array) $coupon->exclude_product_ids), 'usage_limit' => !empty($coupon->usage_limit) ? $coupon->usage_limit : null, 'usage_limit_per_user' => !empty($coupon->usage_limit_per_user) ? $coupon->usage_limit_per_user : null, 'limit_usage_to_x_items' => (int) $coupon->limit_usage_to_x_items, 'free_shipping' => $coupon->enable_free_shipping(), 'product_categories' => array_map('absint', (array) $coupon->product_categories), 'excluded_product_categories' => array_map('absint', (array) $coupon->exclude_product_categories), 'exclude_sale_items' => $coupon->exclude_sale_items(), 'minimum_amount' => wc_format_decimal($coupon->minimum_amount, 2), 'maximum_amount' => wc_format_decimal($coupon->maximum_amount, 2), 'email_restrictions' => $coupon->customer_email, 'used_by' => $coupon->get_used_by()); $context = !empty($request['context']) ? $request['context'] : 'view'; $data = $this->add_additional_fields_to_object($data, $request); $data = $this->filter_response_by_context($data, $context); // Wrap the data in a response object. $response = rest_ensure_response($data); $response->add_links($this->prepare_links($post)); /** * Filter the data for a response. * * The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being * prepared for the response. * * @param WP_REST_Response $response The response object. * @param WP_Post $post Post object. * @param WP_REST_Request $request Request object. */ return apply_filters("woocommerce_rest_prepare_{$this->post_type}", $response, $post, $request); }
/** * Prepare a single coupon output for response. * * @param WP_Post $post Post object. * @param WP_REST_Request $request Request object. * @return WP_REST_Response $data */ public function prepare_item_for_response($post, $request) { // Get the coupon code. $code = wc_get_coupon_code_by_id($post->ID); $coupon = new WC_Coupon($code); $data = array('id' => $coupon->get_id(), 'code' => $coupon->get_code(), 'date_created' => wc_rest_prepare_date_response($post->post_date_gmt), 'date_modified' => wc_rest_prepare_date_response($post->post_modified_gmt), 'discount_type' => $coupon->get_discount_type(), 'description' => $coupon->get_description(), 'amount' => wc_format_decimal($coupon->get_amount(), 2), 'expiry_date' => $coupon->get_expiry_date() ? wc_rest_prepare_date_response($coupon->get_expiry_date()) : null, 'usage_count' => (int) $coupon->get_usage_count(), 'individual_use' => $coupon->get_individual_use(), 'product_ids' => array_map('absint', (array) $coupon->get_product_ids()), 'exclude_product_ids' => array_map('absint', (array) $coupon->get_excluded_product_ids()), 'usage_limit' => $coupon->get_usage_limit() ? $coupon->get_usage_limit() : null, 'usage_limit_per_user' => $coupon->get_usage_limit_per_user() ? $coupon->get_usage_limit_per_user() : null, 'limit_usage_to_x_items' => (int) $coupon->get_limit_usage_to_x_items(), 'free_shipping' => $coupon->get_free_shipping(), 'product_categories' => array_map('absint', (array) $coupon->get_product_categories()), 'excluded_product_categories' => array_map('absint', (array) $coupon->get_excluded_product_categories()), 'exclude_sale_items' => $coupon->get_exclude_sale_items(), 'minimum_amount' => wc_format_decimal($coupon->get_minimum_amount(), 2), 'maximum_amount' => wc_format_decimal($coupon->get_maximum_amount(), 2), 'email_restrictions' => $coupon->get_email_restrictions(), 'used_by' => $coupon->get_used_by()); $context = !empty($request['context']) ? $request['context'] : 'view'; $data = $this->add_additional_fields_to_object($data, $request); $data = $this->filter_response_by_context($data, $context); // Wrap the data in a response object. $response = rest_ensure_response($data); $response->add_links($this->prepare_links($post)); /** * Filter the data for a response. * * The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being * prepared for the response. * * @param WP_REST_Response $response The response object. * @param WP_Post $post Post object. * @param WP_REST_Request $request Request object. */ return apply_filters("woocommerce_rest_prepare_{$this->post_type}", $response, $post, $request); }
/** * Check for user coupons (now that we have billing email). If a coupon is invalid, add an error. * * 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). * * @param array $posted */ 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); } } } } } }