/** * Ensure valid coupon data response. * @since 2.7.0 * @param array $response * @param WC_Coupon $coupon */ protected function check_get_coupon_response($response, $coupon) { $this->assertEquals((int) $coupon->get_id(), $response['id']); $this->assertEquals($coupon->get_code(), $response['code']); $this->assertEquals($coupon->get_discount_type(), $response['type']); $this->assertEquals($coupon->get_amount(), $response['amount']); $this->assertEquals($coupon->get_individual_use(), $response['individual_use']); $this->assertEquals($coupon->get_product_ids(), $response['product_ids']); $this->assertEquals($coupon->get_excluded_product_ids(), $response['exclude_product_ids']); $this->assertEquals((int) $coupon->get_usage_limit(), $response['usage_limit']); $this->assertEquals((int) $coupon->get_usage_limit_per_user(), $response['usage_limit_per_user']); $this->assertEquals((int) $coupon->get_limit_usage_to_x_items(), $response['limit_usage_to_x_items']); $this->assertEquals((int) $coupon->get_usage_count(), $response['usage_count']); $this->assertEquals($coupon->get_expiry_date(), $response['expiry_date']); $this->assertEquals($coupon->get_free_shipping(), $response['enable_free_shipping']); $this->assertEquals($coupon->get_product_categories(), $response['product_category_ids']); $this->assertEquals($coupon->get_excluded_product_categories(), $response['exclude_product_category_ids']); $this->assertEquals($coupon->get_exclude_sale_items(), $response['exclude_sale_items']); $this->assertEquals(wc_format_decimal($coupon->get_minimum_amount(), 2), $response['minimum_amount']); $this->assertEquals(wc_format_decimal($coupon->get_maximum_amount(), 2), $response['maximum_amount']); $this->assertEquals($coupon->get_email_restrictions(), $response['customer_emails']); $this->assertEquals($coupon->get_description(), $response['description']); $this->assertArrayHasKey('created_at', $response); $this->assertArrayHasKey('updated_at', $response); }
/** * Test coupon reading from the DB. * @since 2.7.0 */ function test_coupon_read() { $code = 'coupon-' . time(); $coupon = new WC_Coupon(); $coupon->set_code($code); $coupon->set_description('This is a test coupon.'); $coupon->set_usage_count(5); $coupon->save(); $coupon_id = $coupon->get_id(); $coupon_read = new WC_Coupon($coupon_id); $this->assertEquals(5, $coupon_read->get_usage_count()); $this->assertEquals($code, $coupon_read->get_code()); $this->assertEquals('This is a test coupon.', $coupon_read->get_description()); }
/** * 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); }
/** * Get the coupon for the given ID * * @since 2.1 * @param int $id the coupon ID * @param string $fields fields to include in response * @return array|WP_Error */ public function get_coupon($id, $fields = null) { try { $id = $this->validate_request($id, 'shop_coupon', 'read'); if (is_wp_error($id)) { return $id; } $coupon = new WC_Coupon($id); if (0 === $coupon->get_id()) { throw new WC_API_Exception('woocommerce_api_invalid_coupon_id', __('Invalid coupon ID', 'woocommerce'), 404); } $coupon_data = array('id' => $coupon->get_id(), 'code' => $coupon->get_code(), 'type' => $coupon->get_discount_type(), 'created_at' => $this->server->format_datetime($coupon->get_date_created(), false, true), 'updated_at' => $this->server->format_datetime($coupon->get_date_modified(), false, true), 'amount' => wc_format_decimal($coupon->get_amount(), 2), '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(), 'usage_count' => (int) $coupon->get_usage_count(), 'expiry_date' => $coupon->get_date_expires() ? $this->server->format_datetime($coupon->get_date_expires()) : null, 'enable_free_shipping' => $coupon->get_free_shipping(), 'product_category_ids' => array_map('absint', (array) $coupon->get_product_categories()), 'exclude_product_category_ids' => 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), 'customer_emails' => $coupon->get_email_restrictions(), 'description' => $coupon->get_description()); return array('coupon' => apply_filters('woocommerce_api_coupon_response', $coupon_data, $coupon, $fields, $this->server)); } catch (WC_API_Exception $e) { return new WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode())); } }