WC_REST_Product_Variations_V2_Controller::prepare_object_for_response()publicWC 3.0.0

Prepare a single variation output for response.

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

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

Возвращает

WP_REST_Response.

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

$WC_REST_Product_Variations_V2_Controller = new WC_REST_Product_Variations_V2_Controller();
$WC_REST_Product_Variations_V2_Controller->prepare_object_for_response( $object, $request );
$object(WC_Data) (обязательный)
Object data.
$request(WP_REST_Request) (обязательный)
Request object.

Список изменений

С версии 3.0.0 Введена.

Код WC_REST_Product_Variations_V2_Controller::prepare_object_for_response() WC 9.3.3

public function prepare_object_for_response( $object, $request ) {
	$data = array(
		'id'                    => $object->get_id(),
		'date_created'          => wc_rest_prepare_date_response( $object->get_date_created(), false ),
		'date_created_gmt'      => wc_rest_prepare_date_response( $object->get_date_created() ),
		'date_modified'         => wc_rest_prepare_date_response( $object->get_date_modified(), false ),
		'date_modified_gmt'     => wc_rest_prepare_date_response( $object->get_date_modified() ),
		'description'           => wc_format_content( $object->get_description() ),
		'permalink'             => $object->get_permalink(),
		'sku'                   => $object->get_sku(),
		'price'                 => $object->get_price(),
		'regular_price'         => $object->get_regular_price(),
		'sale_price'            => $object->get_sale_price(),
		'date_on_sale_from'     => wc_rest_prepare_date_response( $object->get_date_on_sale_from(), false ),
		'date_on_sale_from_gmt' => wc_rest_prepare_date_response( $object->get_date_on_sale_from() ),
		'date_on_sale_to'       => wc_rest_prepare_date_response( $object->get_date_on_sale_to(), false ),
		'date_on_sale_to_gmt'   => wc_rest_prepare_date_response( $object->get_date_on_sale_to() ),
		'on_sale'               => $object->is_on_sale(),
		'visible'               => $object->is_visible(),
		'purchasable'           => $object->is_purchasable(),
		'virtual'               => $object->is_virtual(),
		'downloadable'          => $object->is_downloadable(),
		'downloads'             => $this->get_downloads( $object ),
		'download_limit'        => '' !== $object->get_download_limit() ? (int) $object->get_download_limit() : -1,
		'download_expiry'       => '' !== $object->get_download_expiry() ? (int) $object->get_download_expiry() : -1,
		'tax_status'            => $object->get_tax_status(),
		'tax_class'             => $object->get_tax_class(),
		'manage_stock'          => $object->managing_stock(),
		'stock_quantity'        => $object->get_stock_quantity(),
		'in_stock'              => $object->is_in_stock(),
		'backorders'            => $object->get_backorders(),
		'backorders_allowed'    => $object->backorders_allowed(),
		'backordered'           => $object->is_on_backorder(),
		'weight'                => $object->get_weight(),
		'dimensions'            => array(
			'length' => $object->get_length(),
			'width'  => $object->get_width(),
			'height' => $object->get_height(),
		),
		'shipping_class'        => $object->get_shipping_class(),
		'shipping_class_id'     => $object->get_shipping_class_id(),
		'image'                 => current( $this->get_images( $object ) ),
		'attributes'            => $this->get_attributes( $object ),
		'menu_order'            => $object->get_menu_order(),
		'meta_data'             => $object->get_meta_data(),
	);

	$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( $object, $request ) );

	/**
	 * Filter the data for a response.
	 *
	 * The dynamic portion of the hook name, $this->post_type,
	 * refers to object type being prepared for the response.
	 *
	 * @param WP_REST_Response $response The response object.
	 * @param WC_Data          $object   Object data.
	 * @param WP_REST_Request  $request  Request object.
	 */
	return apply_filters( "woocommerce_rest_prepare_{$this->post_type}_object", $response, $object, $request );
}