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

WC_REST_Report_Sales_V1_Controller::prepare_item_for_response() public WC 1.0

Prepare a report sales object for serialization.

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

Хуки из метода
Возвращает

WP_REST_Response. $response Response data.

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

$WC_REST_Report_Sales_V1_Controller = new WC_REST_Report_Sales_V1_Controller();
$WC_REST_Report_Sales_V1_Controller->prepare_item_for_response( $_, $request );
$_(null) (обязательный)
$request(WP_REST_Request) (обязательный)
Request object.

Код WC_REST_Report_Sales_V1_Controller::prepare_item_for_response() WC 4.7.0

<?php
public function prepare_item_for_response( $_, $request ) {
	// Set date filtering.
	$filter = array(
		'period'   => $request['period'],
		'date_min' => $request['date_min'],
		'date_max' => $request['date_max'],
	);
	$this->setup_report( $filter );

	// New customers.
	$users_query = new WP_User_Query(
		array(
			'fields' => array( 'user_registered' ),
			'role'   => 'customer',
		)
	);

	$customers = $users_query->get_results();

	foreach ( $customers as $key => $customer ) {
		if ( strtotime( $customer->user_registered ) < $this->report->start_date || strtotime( $customer->user_registered ) > $this->report->end_date ) {
			unset( $customers[ $key ] );
		}
	}

	$total_customers = count( $customers );
	$report_data     = $this->report->get_report_data();
	$period_totals   = array();

	// Setup period totals by ensuring each period in the interval has data.
	for ( $i = 0; $i <= $this->report->chart_interval; $i++ ) {

		switch ( $this->report->chart_groupby ) {
			case 'day' :
				$time = date( 'Y-m-d', strtotime( "+{$i} DAY", $this->report->start_date ) );
				break;
			default :
				$time = date( 'Y-m', strtotime( "+{$i} MONTH", $this->report->start_date ) );
				break;
		}

		// Set the customer signups for each period.
		$customer_count = 0;
		foreach ( $customers as $customer ) {
			if ( date( ( 'day' == $this->report->chart_groupby ) ? 'Y-m-d' : 'Y-m', strtotime( $customer->user_registered ) ) == $time ) {
				$customer_count++;
			}
 			}

		$period_totals[ $time ] = array(
			'sales'     => wc_format_decimal( 0.00, 2 ),
			'orders'    => 0,
			'items'     => 0,
			'tax'       => wc_format_decimal( 0.00, 2 ),
			'shipping'  => wc_format_decimal( 0.00, 2 ),
			'discount'  => wc_format_decimal( 0.00, 2 ),
			'customers' => $customer_count,
		);
	}

	// add total sales, total order count, total tax and total shipping for each period
	foreach ( $report_data->orders as $order ) {
		$time = ( 'day' === $this->report->chart_groupby ) ? date( 'Y-m-d', strtotime( $order->post_date ) ) : date( 'Y-m', strtotime( $order->post_date ) );

		if ( ! isset( $period_totals[ $time ] ) ) {
			continue;
		}

		$period_totals[ $time ]['sales']    = wc_format_decimal( $order->total_sales, 2 );
		$period_totals[ $time ]['tax']      = wc_format_decimal( $order->total_tax + $order->total_shipping_tax, 2 );
		$period_totals[ $time ]['shipping'] = wc_format_decimal( $order->total_shipping, 2 );
	}

	foreach ( $report_data->order_counts as $order ) {
		$time = ( 'day' === $this->report->chart_groupby ) ? date( 'Y-m-d', strtotime( $order->post_date ) ) : date( 'Y-m', strtotime( $order->post_date ) );

		if ( ! isset( $period_totals[ $time ] ) ) {
			continue;
		}

		$period_totals[ $time ]['orders']   = (int) $order->count;
	}

	// Add total order items for each period.
	foreach ( $report_data->order_items as $order_item ) {
		$time = ( 'day' === $this->report->chart_groupby ) ? date( 'Y-m-d', strtotime( $order_item->post_date ) ) : date( 'Y-m', strtotime( $order_item->post_date ) );

		if ( ! isset( $period_totals[ $time ] ) ) {
			continue;
		}

		$period_totals[ $time ]['items'] = (int) $order_item->order_item_count;
	}

	// Add total discount for each period.
	foreach ( $report_data->coupons as $discount ) {
		$time = ( 'day' === $this->report->chart_groupby ) ? date( 'Y-m-d', strtotime( $discount->post_date ) ) : date( 'Y-m', strtotime( $discount->post_date ) );

		if ( ! isset( $period_totals[ $time ] ) ) {
			continue;
		}

		$period_totals[ $time ]['discount'] = wc_format_decimal( $discount->discount_amount, 2 );
	}

	$sales_data = array(
		'total_sales'       => $report_data->total_sales,
		'net_sales'         => $report_data->net_sales,
		'average_sales'     => $report_data->average_sales,
		'total_orders'      => $report_data->total_orders,
		'total_items'       => $report_data->total_items,
		'total_tax'         => wc_format_decimal( $report_data->total_tax + $report_data->total_shipping_tax, 2 ),
		'total_shipping'    => $report_data->total_shipping,
		'total_refunds'     => $report_data->total_refunds,
		'total_discount'    => $report_data->total_coupons,
		'totals_grouped_by' => $this->report->chart_groupby,
		'totals'            => $period_totals,
		'total_customers'   => $total_customers,
	);

	$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
	$data    = $this->add_additional_fields_to_object( $sales_data, $request );
	$data    = $this->filter_response_by_context( $data, $context );

	// Wrap the data in a response object.
	$response = rest_ensure_response( $data );
	$response->add_links( array(
		'about' => array(
			'href' => rest_url( sprintf( '%s/reports', $this->namespace ) ),
		),
	) );

	/**
	 * Filter a report sales returned from the API.
	 *
	 * Allows modification of the report sales data right before it is returned.
	 *
	 * @param WP_REST_Response $response The response object.
	 * @param stdClass         $data     The original report object.
	 * @param WP_REST_Request  $request  Request used to generate the response.
	 */
	return apply_filters( 'woocommerce_rest_prepare_report_sales', $response, (object) $sales_data, $request );
}