Automattic\WooCommerce\Internal\Admin

Analytics::run_refund_double_count_tool │ public │ WC 1.0

Handle the double-counted refunds fix tool button.

Starts a fix run, or dismisses the tool once a run finished with nothing left to fix. Also reachable through the system status tools REST API, so it re-checks the state.

Method of the class: Analytics{}

No Hooks.

Returns

string. Result message.

Usage

$Analytics = new Analytics();
$Analytics->run_refund_double_count_tool();

Analytics::run_refund_double_count_tool() code WC 11.3.0

public function run_refund_double_count_tool() {
	$state           = self::get_refund_double_count_state();
	$previous_status = '' === $state['status'] ? 'none' : $state['status'];

	if ( self::is_refund_double_count_fix_running( $state ) ) {
		self::record_refund_double_count_tool_run( 'refused_running', $previous_status );
		return __( 'A fix is already in progress, please check back later.', 'woocommerce' );
	}

	if ( self::REFUND_DOUBLE_COUNT_STATUS_COMPLETE === $state['status'] && 0 === $state['unresolved'] ) {
		self::update_refund_double_count_state( array( 'dismissed' => true ) );
		self::record_refund_double_count_tool_run( 'dismissed', $previous_status );
		return __( 'Tool dismissed.', 'woocommerce' );
	}

	if ( self::is_batch_pending_or_running( 'woocommerce_analytics_refund_fix_batch' ) ) {
		self::record_refund_double_count_tool_run( 'refused_full_refund_fix', $previous_status );
		return __( 'The full refund data fix is still running. Please try again once it has finished.', 'woocommerce' );
	}

	$run_id = wp_generate_uuid4();

	self::update_refund_double_count_state(
		array(
			'run_id'       => $run_id,
			'status'       => self::REFUND_DOUBLE_COUNT_STATUS_RUNNING,
			'max_order_id' => self::get_max_order_stats_id(),
			'fixed'        => 0,
			'unresolved'   => 0,
			'batches'      => 0,
			'started_at'   => time(),
			'completed_at' => 0,
		)
	);
	self::schedule_batch( self::REFUND_DOUBLE_COUNT_FIX_HOOK, array( 0, $run_id ) );
	RefundDoubleCountToolNotice::delete_if_not_applicable();
	self::record_refund_double_count_tool_run( 'started', $previous_status );

	return __( 'Checking for affected orders and fixing them in the background. Reload this page to see the progress.', 'woocommerce' );
}