Automattic\WooCommerce\Internal\Admin
Analytics::process_refund_double_count_fix_batch
Process one batch of the double-counted refunds fix.
Re-imports the affected parent orders in the next range of order IDs, checks they were repaired, and schedules the next batch until the highest order ID recorded when the run started is covered. Batches of an older or cancelled run do nothing.
Method of the class: Analytics{}
No Hooks.
Returns
void. Nothing (null).
Usage
$Analytics = new Analytics(); $Analytics->process_refund_double_count_fix_batch( $cursor, $run_id ): void;
- $cursor(int)
- Exclusive lower bound on the parent order ID.
- $run_id(string)
- ID of the run that scheduled the batch.
Default:''
Analytics::process_refund_double_count_fix_batch() Analytics::process refund double count fix batch code WC 11.3.0
public function process_refund_double_count_fix_batch( $cursor = 0, $run_id = '' ): void {
global $wpdb;
$cursor = absint( $cursor );
$run_id = is_string( $run_id ) ? $run_id : '';
if ( ! self::is_current_refund_double_count_run( $run_id ) ) {
return;
}
$max_order_id = self::get_refund_double_count_state()['max_order_id'];
$batch_size = self::get_refund_double_count_batch_size();
$range_end = min( $cursor + self::get_refund_double_count_range_size(), $max_order_id );
$parent_ids = self::get_refund_double_counted_parent_ids(
$wpdb->prepare( 'o.order_id > %d AND o.order_id <= %d', $cursor, $range_end ),
$batch_size
);
// One order that throws must not abandon the run: the batch would never record its
// counters or schedule the next range, leaving the tool stuck on "Checking and fixing".
$failed_ids = array();
foreach ( $parent_ids as $parent_id ) {
try {
OrdersScheduler::import( $parent_id );
} catch ( \Throwable $throwable ) {
$failed_ids[ $parent_id ] = $throwable->getMessage();
}
}
// OrdersScheduler::import() skips some orders silently, so check the result.
$unfixed_ids = array();
if ( $parent_ids ) {
$placeholders = implode( ', ', array_fill( 0, count( $parent_ids ), '%d' ) );
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- Only %d placeholders are interpolated.
$unfixed_ids = self::get_refund_double_counted_parent_ids( $wpdb->prepare( "o.order_id IN ( {$placeholders} )", $parent_ids ) );
}
$unfixed_ids = array_values( array_unique( array_merge( $unfixed_ids, array_keys( $failed_ids ) ) ) );
foreach ( $unfixed_ids as $unfixed_id ) {
wc_get_logger()->warning(
sprintf(
'Could not fix the double-counted refunds of order %1$d: %2$s',
$unfixed_id,
$failed_ids[ $unfixed_id ] ?? 'the order still records more refunds than its own total after re-importing it'
),
array( 'source' => 'wc-analytics-order-import' )
);
}
// A newer run or a cancellation may have happened while the orders were re-imported. Check and
// write the same fresh copy of the state, so this batch never writes into another run's state.
$state = self::get_fresh_refund_double_count_state();
if ( ! self::is_running_state_of( $state, $run_id ) ) {
return;
}
$next_cursor = count( $parent_ids ) >= $batch_size ? (int) end( $parent_ids ) : $range_end;
$is_done = $next_cursor >= $max_order_id;
$changes = array(
'fixed' => $state['fixed'] + count( $parent_ids ) - count( $unfixed_ids ),
'unresolved' => $state['unresolved'] + count( $unfixed_ids ),
'batches' => $state['batches'] + 1,
);
if ( $is_done ) {
$changes['status'] = self::REFUND_DOUBLE_COUNT_STATUS_COMPLETE;
$changes['completed_at'] = time();
}
update_option( self::REFUND_DOUBLE_COUNT_OPTION, array_merge( $state, $changes ), false );
if ( $is_done ) {
self::record_refund_double_count_fix_finished( array_merge( $state, $changes ), self::REFUND_DOUBLE_COUNT_STATUS_COMPLETE );
} else {
self::schedule_batch( self::REFUND_DOUBLE_COUNT_FIX_HOOK, array( $next_cursor, $run_id ), 5 );
}
}