WC_Tracker::get_orders_origins()private staticWC 1.0

Get orders origin details.

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

Хуков нет.

Возвращает

Массив.

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

$result = WC_Tracker::get_orders_origins();

Код WC_Tracker::get_orders_origins() WC 8.7.0

private static function get_orders_origins() {
	global $wpdb;

	if ( OrderUtil::custom_orders_table_usage_is_enabled() ) {
		$op_table_name = OrdersTableDataStore::get_operational_data_table_name();
		// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		$orders_origin = $wpdb->get_results(
			"
			SELECT created_via as origin, COUNT( order_id ) as count
			FROM $op_table_name
			GROUP BY created_via;
			"
		);
		// phpcs:enable
	} else {
		$orders_origin = $wpdb->get_results(
			"
			SELECT
				meta_value as origin, COUNT( DISTINCT ( orders.id ) ) as count
			FROM
				$wpdb->posts orders
			LEFT JOIN
				$wpdb->postmeta order_meta ON order_meta.post_id = orders.id
			WHERE
				meta_key = '_created_via'
			GROUP BY
				meta_value;
		"
		);
	}

	// 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 identifiers that may sometimes be part of an origin.
	// For example, two origins like 'Import #123' and 'Import ** #78' would both have a group_key 'Import **'.
	$orders_and_origins = self::extract_group_key(
		// Convert into an associative array with the origin as key.
		array_reduce(
			$orders_origin,
			function( $result, $item ) {
				$key = $item->origin;

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

	$orders_by_origin = array();

	// Aggregate using group_key.
	foreach ( $orders_and_origins as $origin ) {
		$key = strtolower( $origin->group_key );

		if ( array_key_exists( $key, $orders_by_origin ) ) {
			$orders_by_origin[ $key ] = $orders_by_origin[ $key ] + (int) $origin->count;
		} else {
			$orders_by_origin[ $key ] = (int) $origin->count;
		}
	}

	return array( 'created_via' => $orders_by_origin );
}