Automattic\WooCommerce\Internal\ProductFeed\Feed

ProductWalker::iterate │ private │ WC 1.0

Iterates through a batch of products.

Method of the class: ProductWalker{}

No Hooks.

Returns

\stdClass. The result of the query with properties: products, total, max_num_pages.

Usage

// private - for code of main (parent) class only
$result = $this->iterate( $args, $after_id, $limit ): \stdClass;
$args(array)
The arguments to pass to wc_get_products().
Default: array()
$after_id(int)
Only include products with an ID greater than this one.
$limit(int)
The maximum number of products to iterate through.
Default: 100

ProductWalker::iterate() code WC 11.3.0

private function iterate( array $args = array(), int $after_id = 0, int $limit = 100 ): \stdClass {
	// WP_Query has no "ID greater than" argument, so add it to the WHERE clause of this query only.
	$after_id_where = static function ( $where, $query ) {
		global $wpdb;

		if ( ! $query instanceof \WP_Query || '' === $query->get( self::AFTER_ID_QUERY_VAR ) ) {
			return $where;
		}

		return $where . $wpdb->prepare( " AND {$wpdb->posts}.ID > %d", (int) $query->get( self::AFTER_ID_QUERY_VAR ) );
	};

	add_filter( 'posts_where', $after_id_where, 10, 2 );
	try {
		/**
		 * Result is always stdClass when paginate=true.
		 *
		 * @var \stdClass $result
		 */
		$result = $this->product_loader->get_products(
			array_merge(
				$args,
				array(
					self::AFTER_ID_QUERY_VAR => $after_id,
					'limit'                  => $limit,
					'paginate'               => true,
				)
			)
		);
	} finally {
		remove_filter( 'posts_where', $after_id_where, 10 );
	}

	foreach ( $result->products as $product ) {
		$mapped_data = $this->mapper->map_product( $product );

		if ( ! empty( $this->validator->validate_entry( $mapped_data, $product ) ) ) {
			continue;
		}

		$this->feed->add_entry( $mapped_data );
	}

	return $result;
}