/** * 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) { $code = wc_get_coupon_code_by_id($post->ID); $coupon = new WC_Coupon($code); $data = $coupon->get_data(); $format_decimal = array('amount', 'minimum_amount', 'maximum_amount'); $format_date = array('date_created', 'date_modified', 'date_expires'); $format_null = array('usage_limit', 'usage_limit_per_user', 'limit_usage_to_x_items'); // Format decimal values. foreach ($format_decimal as $key) { $data[$key] = wc_format_decimal($data[$key], 2); } // Format date values. foreach ($format_date as $key) { $data[$key] = $data[$key] ? wc_rest_prepare_date_response(get_gmt_from_date(date('Y-m-d H:i:s', $data[$key]))) : null; } // Format null values. foreach ($format_null as $key) { $data[$key] = $data[$key] ? $data[$key] : null; } $context = !empty($request['context']) ? $request['context'] : 'view'; $data = $this->add_additional_fields_to_object($data, $request); $data = $this->filter_response_by_context($data, $context); $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); }