Automattic\WooCommerce\Internal\Admin

Analytics::ajax_check_refund_fix_neededpublicWC 10.8.0

AJAX handler: checks whether the store has analytics order stats rows that look like unprocessed full refunds.

Method of the class: Analytics{}

No Hooks.

Returns

null. Nothing (null).

Usage

$Analytics = new Analytics();
$Analytics->ajax_check_refund_fix_needed(): void;

Changelog

Since 10.8.0 Introduced.

Analytics::ajax_check_refund_fix_needed() code WC 10.9.4

public function ajax_check_refund_fix_needed(): void {
	check_ajax_referer( 'woocommerce_refund_fix_check', 'nonce' );

	if ( ! current_user_can( 'manage_woocommerce' ) ) {
		wp_send_json_error( array( 'message' => __( 'Insufficient permissions.', 'woocommerce' ) ), 403 );
	}

	global $wpdb;

	// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
	$has_affected = $wpdb->get_var(
		"SELECT order_stats.order_id
		FROM {$wpdb->prefix}wc_order_stats AS order_stats
		INNER JOIN {$wpdb->prefix}wc_order_stats AS parent_stats ON order_stats.parent_id = parent_stats.order_id
		WHERE order_stats.total_sales < 0
			AND order_stats.total_sales = order_stats.net_total
			AND order_stats.total_sales != order_stats.shipping_total
			AND order_stats.total_sales != order_stats.tax_total
			AND (parent_stats.shipping_total > 0 OR parent_stats.tax_total > 0)
		LIMIT 1"
	);

	if ( $wpdb->last_error ) {
		wp_send_json_error(
			array(
				'code'    => 'db_error',
				'message' => $wpdb->last_error,
			),
			500
		);
	}

	$fix_in_progress = ! empty(
		as_get_scheduled_actions(
			array(
				'hook'     => 'woocommerce_analytics_refund_fix_batch',
				'status'   => array( \ActionScheduler_Store::STATUS_PENDING, \ActionScheduler_Store::STATUS_RUNNING ),
				'per_page' => 1,
				'orderby'  => 'none',
			),
			'ids'
		)
	);

	wp_send_json_success(
		array(
			'needs_fix'       => ! empty( $has_affected ),
			'fix_in_progress' => $fix_in_progress,
		)
	);
}