WordPress как на ладони
Недорогой хостинг для сайтов на WordPress: wordpress.jino.ru Получай пассивный доход от сайта
функция не описана

WC_REST_Orders_V2_Controller::get_formatted_item_data() protected WC 3.0.0

Get formatted item data.

{} Это метод класса: WC_REST_Orders_V2_Controller{}

Хуков нет.

Возвращает

Массив.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->get_formatted_item_data( $order );
$order(WC_Order) (обязательный)
WC_Data instance.

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

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

Код WC_REST_Orders_V2_Controller::get_formatted_item_data() WC 5.5.2

<?php
protected function get_formatted_item_data( $order ) {
	$extra_fields      = array( 'meta_data', 'line_items', 'tax_lines', 'shipping_lines', 'fee_lines', 'coupon_lines', 'refunds' );
	$format_decimal    = array( 'discount_total', 'discount_tax', 'shipping_total', 'shipping_tax', 'shipping_total', 'shipping_tax', 'cart_tax', 'total', 'total_tax' );
	$format_date       = array( 'date_created', 'date_modified', 'date_completed', 'date_paid' );
	// These fields are dependent on other fields.
	$dependent_fields = array(
		'date_created_gmt'   => 'date_created',
		'date_modified_gmt'  => 'date_modified',
		'date_completed_gmt' => 'date_completed',
		'date_paid_gmt'      => 'date_paid',
	);

	$format_line_items = array( 'line_items', 'tax_lines', 'shipping_lines', 'fee_lines', 'coupon_lines' );

	// Only fetch fields that we need.
	$fields = $this->get_fields_for_response( $this->request );
	foreach ( $dependent_fields as $field_key => $dependency ) {
		if ( in_array( $field_key, $fields ) && ! in_array( $dependency, $fields ) ) {
			$fields[] = $dependency;
		}
	}

	$extra_fields      = array_intersect( $extra_fields, $fields );
	$format_decimal    = array_intersect( $format_decimal, $fields );
	$format_date       = array_intersect( $format_date, $fields );

	$format_line_items = array_intersect( $format_line_items, $fields );

	$data = $order->get_base_data();

	// Add extra data as necessary.
	foreach ( $extra_fields as $field ) {
		switch ( $field ) {
			case 'meta_data':
				$data['meta_data'] = $order->get_meta_data();
				break;
			case 'line_items':
				$data['line_items'] = $order->get_items( 'line_item' );
				break;
			case 'tax_lines':
				$data['tax_lines'] = $order->get_items( 'tax' );
				break;
			case 'shipping_lines':
				$data['shipping_lines'] = $order->get_items( 'shipping' );
				break;
			case 'fee_lines':
				$data['fee_lines'] = $order->get_items( 'fee' );
				break;
			case 'coupon_lines':
				$data['coupon_lines'] = $order->get_items( 'coupon' );
				break;
			case 'refunds':
				$data['refunds'] = array();
				foreach ( $order->get_refunds() as $refund ) {
					$data['refunds'][] = array(
						'id'     => $refund->get_id(),
						'reason' => $refund->get_reason() ? $refund->get_reason() : '',
						'total'  => '-' . wc_format_decimal( $refund->get_amount(), $this->request['dp'] ),
					);
				}
				break;
		}
	}

	// Format decimal values.
	foreach ( $format_decimal as $key ) {
		$data[ $key ] = wc_format_decimal( $data[ $key ], $this->request['dp'] );
	}

	// Format date values.
	foreach ( $format_date as $key ) {
		$datetime              = $data[ $key ];
		$data[ $key ]          = wc_rest_prepare_date_response( $datetime, false );
		$data[ $key . '_gmt' ] = wc_rest_prepare_date_response( $datetime );
	}

	// Format the order status.
	$data['status'] = 'wc-' === substr( $data['status'], 0, 3 ) ? substr( $data['status'], 3 ) : $data['status'];

	// Format line items.
	foreach ( $format_line_items as $key ) {
		$data[ $key ] = array_values( array_map( array( $this, 'get_order_item_data' ), $data[ $key ] ) );
	}

	$allowed_fields = array(
		'id',
		'parent_id',
		'number',
		'order_key',
		'created_via',
		'version',
		'status',
		'currency',
		'date_created',
		'date_created_gmt',
		'date_modified',
		'date_modified_gmt',
		'discount_total',
		'discount_tax',
		'shipping_total',
		'shipping_tax',
		'cart_tax',
		'total',
		'total_tax',
		'prices_include_tax',
		'customer_id',
		'customer_ip_address',
		'customer_user_agent',
		'customer_note',
		'billing',
		'shipping',
		'payment_method',
		'payment_method_title',
		'transaction_id',
		'date_paid',
		'date_paid_gmt',
		'date_completed',
		'date_completed_gmt',
		'cart_hash',
		'meta_data',
		'line_items',
		'tax_lines',
		'shipping_lines',
		'fee_lines',
		'coupon_lines',
		'refunds',
	);

	$data = array_intersect_key( $data, array_flip( $allowed_fields ) );

	return $data;
}