本文整理汇总了PHP中WC_Coupon::get_used_by方法的典型用法代码示例。如果您正苦于以下问题:PHP WC_Coupon::get_used_by方法的具体用法?PHP WC_Coupon::get_used_by怎么用?PHP WC_Coupon::get_used_by使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WC_Coupon
的用法示例。
在下文中一共展示了WC_Coupon::get_used_by方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: prepare_item_for_response
/**
* 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);
}
示例2: prepare_item_for_response
/**
* 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);
}
示例3: check_customer_coupons
/**
* 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);
}
}
}
}
}
}