WC_Tracker::get_orders_by_gateway()private staticWC 1.0

Get order details by gateway.

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

Хуков нет.

Возвращает

Массив.

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

$result = WC_Tracker::get_orders_by_gateway();

Код WC_Tracker::get_orders_by_gateway() WC 8.9.3

private static function get_orders_by_gateway() {
	global $wpdb;

	if ( OrderUtil::custom_orders_table_usage_is_enabled() ) {
		$orders_table = OrdersTableDataStore::get_orders_table_name();
		// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		$orders_and_gateway_details = $wpdb->get_results(
			"
			SELECT payment_method AS gateway, currency AS currency, SUM( total_amount ) AS totals, count( id ) AS counts
			FROM $orders_table
			WHERE status IN ( 'wc-completed', 'wc-processing', 'wc-refunded' )
			GROUP BY gateway, currency;
			"
		);
		// phpcs:enable
	} else {
		$orders_and_gateway_details = $wpdb->get_results(
			"
			SELECT
				gateway, currency, SUM(total) AS totals, COUNT(order_id) AS counts
			FROM (
				SELECT
					orders.id AS order_id,
					MAX(CASE WHEN meta_key = '_payment_method' THEN meta_value END) gateway,
					MAX(CASE WHEN meta_key = '_order_total' THEN meta_value END) total,
					MAX(CASE WHEN meta_key = '_order_currency' THEN meta_value END) currency
				FROM
					{$wpdb->prefix}posts orders
				LEFT JOIN
					{$wpdb->prefix}postmeta order_meta ON order_meta.post_id = orders.id
				WHERE orders.post_type = 'shop_order'
					AND orders.post_status in ( 'wc-completed', 'wc-processing', 'wc-refunded' )
					AND meta_key in( '_payment_method','_order_total','_order_currency')
				GROUP BY orders.id
			) order_gateways
			GROUP BY gateway, currency
			"
		);
	}

	$orders_by_gateway_currency = array();

	// The associative array that is created as the result of array_reduce is passed to extract_group_key()
	// This function has the logic that will remove specific transaction identifiers that may sometimes be part of a
	// payment method. For example, two payments methods like 'WooDataPay ** #123' and 'WooDataPay ** #78' would
	// both have the same group_key 'WooDataPay **'.
	$orders_by_gateway = self::extract_group_key(
		// Convert into an associative array with a combination of currency and gateway as key.
		array_reduce(
			$orders_and_gateway_details,
			function( $result, $item ) {
				$item->gateway = preg_replace( '/\s+/', ' ', $item->gateway );

				// Introduce currency as a prefix for the key.
				$key = $item->currency . '==' . $item->gateway;

				$result[ $key ] = $item;
				return $result;
			},
			array()
		),
		'gateway'
	);

	// Aggregate using group_key.
	foreach ( $orders_by_gateway as $orders_details ) {
		$gkey = $orders_details->group_key;

		// Remove currency as prefix of key for backward compatibility.
		if ( str_contains( $gkey, '==' ) ) {
			$tokens = preg_split( '/==/', $gkey );
			$key    = $tokens[1];
		} else {
			$key = $gkey;
		}

		$key = str_replace( array( 'payment method', 'payment gateway', 'gateway' ), '', strtolower( $key ) );
		$key = trim( preg_replace( '/[: ,#*\-_]+/', ' ', $key ) );

		// Add currency as postfix of gateway for backward compatibility.
		$key       = 'gateway_' . $key . '_' . $orders_details->currency;
		$count_key = $key . '_count';
		$total_key = $key . '_total';

		if ( array_key_exists( $count_key, $orders_by_gateway_currency ) || array_key_exists( $total_key, $orders_by_gateway_currency ) ) {
			$orders_by_gateway_currency[ $count_key ] = $orders_by_gateway_currency[ $count_key ] + $orders_details->counts;
			$orders_by_gateway_currency[ $total_key ] = $orders_by_gateway_currency[ $total_key ] + $orders_details->totals;
		} else {
			$orders_by_gateway_currency[ $count_key ] = $orders_details->counts;
			$orders_by_gateway_currency[ $total_key ] = $orders_details->totals;
		}
	}

	return $orders_by_gateway_currency;
}