WC_REST_Legacy_Coupons_Controller::prepare_item_for_response()publicWC 1.0

Deprecated from version 3.0.0. It is no longer supported and can be removed in future releases. It is recommended to replace this function with the same one.

Prepare a single coupon output for response.

Method of the class: WC_REST_Legacy_Coupons_Controller{}

Hooks from the method

Return

WP_REST_Response. $data

Usage

$WC_REST_Legacy_Coupons_Controller = new WC_REST_Legacy_Coupons_Controller();
$WC_REST_Legacy_Coupons_Controller->prepare_item_for_response( $post, $request );
$post(WP_Post) (required)
Post object.
$request(WP_REST_Request) (required)
Request object.

Changelog

Deprecated since 3.0.0

WC_REST_Legacy_Coupons_Controller::prepare_item_for_response() code WC 8.7.0

public function prepare_item_for_response( $post, $request ) {
	$coupon = new WC_Coupon( (int) $post->ID );
	$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, $request ) );

	/**
	 * 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 );
}