WC_REST_Coupons_V1_Controller::prepare_item_for_response()publicWC 1.0

Prepare a single coupon output for response.

Метод класса: WC_REST_Coupons_V1_Controller{}

Хуки из метода

Возвращает

WP_REST_Response. $data

Использование

$WC_REST_Coupons_V1_Controller = new WC_REST_Coupons_V1_Controller();
$WC_REST_Coupons_V1_Controller->prepare_item_for_response( $post, $request );
$post(WP_Post) (обязательный)
Post object.
$request(WP_REST_Request) (обязательный)
Request object.

Код WC_REST_Coupons_V1_Controller::prepare_item_for_response() 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' );
	$format_date_utc = array( 'date_expires' );
	$format_null     = array( 'usage_limit', 'usage_limit_per_user' );

	// 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( $_data[ $key ], false ) : null;
	}
	foreach ( $format_date_utc as $key ) {
		$_data[ $key ] = $_data[ $key ] ? wc_rest_prepare_date_response( $_data[ $key ] ) : null;
	}

	// Format null values.
	foreach ( $format_null as $key ) {
		$_data[ $key ] = $_data[ $key ] ? $_data[ $key ] : null;
	}

	$data = array(
		'id'                          => $_data['id'],
		'code'                        => $_data['code'],
		'date_created'                => $_data['date_created'],
		'date_modified'               => $_data['date_modified'],
		'discount_type'               => $_data['discount_type'],
		'description'                 => $_data['description'],
		'amount'                      => $_data['amount'],
		'expiry_date'                 => $_data['date_expires'],
		'usage_count'                 => $_data['usage_count'],
		'individual_use'              => $_data['individual_use'],
		'product_ids'                 => $_data['product_ids'],
		'exclude_product_ids'         => $_data['excluded_product_ids'],
		'usage_limit'                 => $_data['usage_limit'],
		'usage_limit_per_user'        => $_data['usage_limit_per_user'],
		'limit_usage_to_x_items'      => $_data['limit_usage_to_x_items'],
		'free_shipping'               => $_data['free_shipping'],
		'product_categories'          => $_data['product_categories'],
		'excluded_product_categories' => $_data['excluded_product_categories'],
		'exclude_sale_items'          => $_data['exclude_sale_items'],
		'minimum_amount'              => $_data['minimum_amount'],
		'maximum_amount'              => $_data['maximum_amount'],
		'email_restrictions'          => $_data['email_restrictions'],
		'used_by'                     => $_data['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 );
	$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 );
}