Automattic\WooCommerce\Internal\ProductFeed\Feed

ProductWalker::walk_chunk │ public │ WC 11.3.0

Walks through a chunk of products, optionally limited to a bounded number of batches.

The walker does not own the feed lifecycle, so a feed can be written across several processes: walk a chunk, then resume the next one from the returned last_product_id. Resuming from an ID rather than a page keeps products from being skipped when earlier ones are removed in between.

Method of the class: ProductWalker{}

No Hooks.

Returns

WalkerProgress. Items/batches processed here, plus the total_count and total_batch_count remaining when this chunk started, so the caller knows whether the feed is complete.

Usage

$ProductWalker = new ProductWalker();
$ProductWalker->walk_chunk( ?callable $callback, $resume_after_product_id, $max_batches ): WalkerProgress;
?callable $callback
.
Default: null
$resume_after_product_id(int)
Resume after this product ID, usually the previous chunk's last_product_id; 0 starts from the beginning.
$max_batches(int)
The maximum number of batches to process in this call.
Default: PHP_INT_MAX

Changelog

Since 11.3.0 Introduced.

ProductWalker::walk_chunk() code WC 11.3.0

public function walk_chunk( ?callable $callback = null, int $resume_after_product_id = 0, int $max_batches = PHP_INT_MAX ): WalkerProgress {
	$after_id = max( 0, $resume_after_product_id );
	if ( $max_batches < 1 ) {
		$max_batches = 1;
	}

	$progress = null;

	// Check how much memory is available at first.
	$initial_available_memory = $this->memory_manager->get_available_memory();

	$batches_processed = 0;
	do {
		$result   = $this->iterate( $this->query_args, $after_id, $this->per_page );
		$iterated = count( $result->products );

		// Only build the progress object once, so the totals reflect what remained when this call started.
		if ( is_null( $progress ) ) {
			$progress = WalkerProgress::from_wc_get_products_result( $result );
		}
		if ( $iterated > 0 ) {
			$after_id = end( $result->products )->get_id();
		}
		$progress->last_product_id  = $after_id;
		$progress->processed_items += $iterated;
		++$progress->processed_batches;
		++$batches_processed;

		if ( is_callable( $callback ) && $iterated > 0 ) {
			$callback( $progress );
		}

		if ( $this->time_limit > 0 ) {
			set_time_limit( $this->time_limit );
		}

		// We don't want to use more than half of the available memory at the beginning of the script.
		$current_memory = $this->memory_manager->get_available_memory();
		if ( $initial_available_memory - $current_memory >= $initial_available_memory / 2 ) {
			$this->memory_manager->flush_caches();
		}
	} while (
		// If `wc_get_products()` returns less than the batch size, it was the last page.
		$iterated === $this->per_page

		// Stop once this call has processed its requested number of batches.
		&& $batches_processed < $max_batches

		// For the cases where the above are true, make sure that we do not exceed the total number of pages.
		&& ( $progress->total_batch_count <= 0 || $batches_processed < $progress->total_batch_count )
	);

	// The do-while body always executes at least once and assigns $progress on the first iteration.
	return $progress;
}