Commit 42e5a8b2faf for woocommerce

commit 42e5a8b2fafeb62a8f0d55ddb8deefd39dec82ae
Author: Aarón Fas <yo@aaron.com.es>
Date:   Tue Nov 17 17:02:00 2026 +0100

    Return totals from product, order and payment gateway queries (#71011)

    * Return total from products-query and orders-query abilities

    Both abilities returned total_pages but not the number of matching results. Use the total the underlying paginated query already reports, in the output schemas too.

    Co-authored-by: matticbot <matticbot@users.noreply.github.com>
    Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

    * Fix total tests: per-storage-mode filters, HPOS coverage, products schema check

    Co-authored-by: matticbot <matticbot@users.noreply.github.com>
    Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

    * Fix query page counts on empty result pages

    Derive product and order page counts from the matching total after the empty-page fallback. Both posts and HPOS queries can skip counts when there are no rows, so retaining their original page count contradicts the recovered total.

    Assert page counts beyond the last page, for zero matches and ID lookups, and through extension filters in both order storage modes. Reuse the existing count query without adding database work.

    * Preserve extension-controlled query pagination

    Use datastore page counts so extension filters that change the page size remain authoritative. On empty later pages, retain the original limit in the existing first-page ID query and recover both the matching total and page count without another query.

    Cover conditional page-size overrides on full, partial and empty pages for products and both order storage modes. Keep the requested per_page response and existing matching-filter behavior unchanged.

    * Add matching totals to the payment gateways query

    Return the loaded gateway count as total and describe it in the output schema so collection query responses use consistent metadata. Since this ability returns all matches at once, total equals per_page without adding queries or pagination inputs.

    Remove the payment-gateway schema exception and cover list, ID lookup and empty results while retaining existing gateway fields and credential protections.

    * Include payment gateway totals in the changelog

    Describe the expanded collection-total change now that payment gateways return the same total metadata as product and order queries.

    ---------

    Co-authored-by: matticbot <matticbot@users.noreply.github.com>
    Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>

diff --git a/plugins/woocommerce/changelog/add-abilities-query-total b/plugins/woocommerce/changelog/add-abilities-query-total
new file mode 100644
index 00000000000..24cb4a6d489
--- /dev/null
+++ b/plugins/woocommerce/changelog/add-abilities-query-total
@@ -0,0 +1,4 @@
+Significance: minor
+Type: add
+
+Return matching result totals from product, order, and payment gateway query abilities.
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/OrdersQuery.php b/plugins/woocommerce/src/Internal/Abilities/Domain/OrdersQuery.php
index 96e42472695..81a19ad85db 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/OrdersQuery.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/OrdersQuery.php
@@ -49,7 +49,7 @@ class OrdersQuery extends AbstractDomainAbility implements AbilityDefinition {
 			),
 			'category'            => 'woocommerce',
 			'input_schema'        => self::get_input_schema(),
-			'output_schema'       => self::get_collection_output_schema( 'orders', self::get_order_output_schema() ),
+			'output_schema'       => self::get_output_schema(),
 			'execute_callback'    => array( __CLASS__, 'execute' ),
 			'permission_callback' => array( __CLASS__, 'can_query_orders' ),
 			'meta'                => array(
@@ -134,6 +134,7 @@ class OrdersQuery extends AbstractDomainAbility implements AbilityDefinition {

 			return array(
 				'orders'      => $orders,
+				'total'       => 1,
 				'total_pages' => 1,
 				'page'        => 1,
 				'per_page'    => 1,
@@ -225,7 +226,23 @@ class OrdersQuery extends AbstractDomainAbility implements AbilityDefinition {

 		$results = wc_get_orders( $args );
 		$orders  = is_object( $results ) && isset( $results->orders ) ? $results->orders : array();
-		$orders  = array_values(
+		$total   = is_object( $results ) && isset( $results->total ) ? (int) $results->total : count( $orders );
+		$pages   = is_object( $results ) && isset( $results->max_num_pages ) ? (int) $results->max_num_pages : (int) ceil( $total / $per_page );
+
+		// Queries skip their count when the requested page has no rows.
+		if ( 0 === $total && empty( $orders ) && $page > 1 ) {
+			$count_args = array_merge(
+				$args,
+				array(
+					'page'   => 1,
+					'return' => 'ids',
+				)
+			);
+			$count      = wc_get_orders( $count_args );
+			$total      = is_object( $count ) && isset( $count->total ) ? (int) $count->total : 0;
+			$pages      = is_object( $count ) && isset( $count->max_num_pages ) ? (int) $count->max_num_pages : (int) ceil( $total / $per_page );
+		}
+		$orders = array_values(
 			array_filter(
 				$orders,
 				static function ( $order ): bool {
@@ -233,7 +250,6 @@ class OrdersQuery extends AbstractDomainAbility implements AbilityDefinition {
 				}
 			)
 		);
-		$pages   = is_object( $results ) && isset( $results->max_num_pages ) ? (int) $results->max_num_pages : ( count( $orders ) > 0 ? 1 : 0 );

 		$orders = self::format_orders_for_response( $orders, $includes );
 		if ( is_wp_error( $orders ) ) {
@@ -242,6 +258,7 @@ class OrdersQuery extends AbstractDomainAbility implements AbilityDefinition {

 		return array(
 			'orders'      => $orders,
+			'total'       => $total,
 			'total_pages' => $pages,
 			'page'        => $page,
 			'per_page'    => $per_page,
@@ -278,6 +295,21 @@ class OrdersQuery extends AbstractDomainAbility implements AbilityDefinition {
 		return $input['type'] ?? 'shop_order';
 	}

+	/**
+	 * Get the ability output schema.
+	 *
+	 * @return array
+	 */
+	private static function get_output_schema(): array {
+		$schema                        = self::get_collection_output_schema( 'orders', self::get_order_output_schema() );
+		$schema['properties']['total'] = array(
+			'type'        => 'integer',
+			'description' => __( 'Total number of matching orders across all pages.', 'woocommerce' ),
+		);
+
+		return $schema;
+	}
+
 	/**
 	 * Get the ability input schema.
 	 *
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/PaymentGatewaysQuery.php b/plugins/woocommerce/src/Internal/Abilities/Domain/PaymentGatewaysQuery.php
index 27ba76a3580..ba0559af53e 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/PaymentGatewaysQuery.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/PaymentGatewaysQuery.php
@@ -91,6 +91,7 @@ class PaymentGatewaysQuery extends AbstractDomainAbility implements AbilityDefin

 		return array(
 			'gateways'    => $items,
+			'total'       => count( $items ),
 			'total_pages' => $items ? 1 : 0,
 			'page'        => 1,
 			'per_page'    => count( $items ),
@@ -134,6 +135,12 @@ class PaymentGatewaysQuery extends AbstractDomainAbility implements AbilityDefin
 	 * @return array
 	 */
 	private static function get_output_schema(): array {
-		return self::get_collection_output_schema( 'gateways', self::get_payment_gateway_item_schema() );
+		$schema                        = self::get_collection_output_schema( 'gateways', self::get_payment_gateway_item_schema() );
+		$schema['properties']['total'] = array(
+			'type'        => 'integer',
+			'description' => __( 'Total number of matching payment gateways.', 'woocommerce' ),
+		);
+
+		return $schema;
 	}
 }
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/ProductsQuery.php b/plugins/woocommerce/src/Internal/Abilities/Domain/ProductsQuery.php
index bdd7859aac2..734631f47f0 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/ProductsQuery.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/ProductsQuery.php
@@ -63,7 +63,7 @@ class ProductsQuery extends AbstractDomainAbility implements AbilityDefinition {
 			),
 			'category'            => 'woocommerce',
 			'input_schema'        => self::get_input_schema(),
-			'output_schema'       => self::get_collection_output_schema( 'products', self::get_product_output_schema() ),
+			'output_schema'       => self::get_output_schema(),
 			'execute_callback'    => array( __CLASS__, 'execute' ),
 			'permission_callback' => array( __CLASS__, 'can_query_products' ),
 			'meta'                => array(
@@ -112,6 +112,7 @@ class ProductsQuery extends AbstractDomainAbility implements AbilityDefinition {

 			return array(
 				'products'    => $products,
+				'total'       => 1,
 				'total_pages' => 1,
 				'page'        => 1,
 				'per_page'    => 1,
@@ -185,10 +186,25 @@ class ProductsQuery extends AbstractDomainAbility implements AbilityDefinition {

 		add_filter( 'posts_where', array( __CLASS__, 'limit_to_parent_types' ), 10, 2 );
 		$results = wc_get_products( $args );
-		remove_filter( 'posts_where', array( __CLASS__, 'limit_to_parent_types' ) );

 		$products = is_object( $results ) && isset( $results->products ) ? $results->products : array();
-		$pages    = is_object( $results ) && isset( $results->max_num_pages ) ? (int) $results->max_num_pages : ( count( $products ) > 0 ? 1 : 0 );
+		$total    = is_object( $results ) && isset( $results->total ) ? (int) $results->total : count( $products );
+		$pages    = is_object( $results ) && isset( $results->max_num_pages ) ? (int) $results->max_num_pages : (int) ceil( $total / $per_page );
+
+		// Queries skip their count when the requested page has no rows.
+		if ( 0 === $total && empty( $products ) && $page > 1 ) {
+			$count_args = array_merge(
+				$args,
+				array(
+					'page'   => 1,
+					'return' => 'ids',
+				)
+			);
+			$count      = wc_get_products( $count_args );
+			$total      = is_object( $count ) && isset( $count->total ) ? (int) $count->total : 0;
+			$pages      = is_object( $count ) && isset( $count->max_num_pages ) ? (int) $count->max_num_pages : (int) ceil( $total / $per_page );
+		}
+		remove_filter( 'posts_where', array( __CLASS__, 'limit_to_parent_types' ) );

 		$image_ids = array_unique( array_merge( array(), ...array_map( array( __CLASS__, 'get_product_image_ids' ), $products ) ) );
 		if ( ! empty( $image_ids ) ) {
@@ -202,6 +218,7 @@ class ProductsQuery extends AbstractDomainAbility implements AbilityDefinition {

 		return array(
 			'products'    => $products,
+			'total'       => $total,
 			'total_pages' => $pages,
 			'page'        => $page,
 			'per_page'    => $per_page,
@@ -261,6 +278,21 @@ class ProductsQuery extends AbstractDomainAbility implements AbilityDefinition {
 		return (bool) wc_rest_check_post_permissions( 'product', 'read', $product_id );
 	}

+	/**
+	 * Get the ability output schema.
+	 *
+	 * @return array
+	 */
+	private static function get_output_schema(): array {
+		$schema                        = self::get_collection_output_schema( 'products', self::get_product_output_schema() );
+		$schema['properties']['total'] = array(
+			'type'        => 'integer',
+			'description' => __( 'Total number of matching products across all pages.', 'woocommerce' ),
+		);
+
+		return $schema;
+	}
+
 	/**
 	 * Get the ability input schema.
 	 *
diff --git a/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php b/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php
index 2c74ae07767..f49100b3dcb 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php
@@ -461,7 +461,8 @@ class AbilitiesLoaderTest extends \WC_Unit_Test_Case {
 			$this->assertArrayHasKey( 'total_pages', $properties );
 			$this->assertArrayHasKey( 'page', $properties );
 			$this->assertArrayHasKey( 'per_page', $properties );
-			$this->assertArrayNotHasKey( 'total', $properties );
+			$this->assertSame( 'integer', $properties['total']['type'] );
+			$this->assertNotEmpty( $properties['total']['description'] );
 			$this->assertNotEmpty( $properties[ $collection_key ]['description'] ?? '' );
 			$this->assertNotEmpty( $properties['total_pages']['description'] ?? '' );
 			$this->assertNotEmpty( $properties['page']['description'] ?? '' );
@@ -5779,6 +5780,27 @@ class AbilitiesLoaderTest extends \WC_Unit_Test_Case {
 		$this->assertSame( 'Direct bank transfer', array_column( $result['gateways'], 'method_title', 'id' )['bacs'] );
 		$this->assertSame( 1, $result['total_pages'] );
 		$this->assertSame( count( $result['gateways'] ), $result['per_page'] );
+		$this->assertSame( count( $result['gateways'] ), $result['total'] );
+	}
+
+	/**
+	 * @testdox Should return zero totals when no payment gateways are loaded.
+	 */
+	public function test_payment_gateways_query_returns_zero_total_without_gateways(): void {
+		add_filter( 'woocommerce_payment_gateways', '__return_empty_array' );
+		try {
+			self::reload_payment_gateways();
+			$result = wp_get_ability( 'woocommerce/payment-gateways-query' )->execute( array() );
+		} finally {
+			remove_filter( 'woocommerce_payment_gateways', '__return_empty_array' );
+			self::reload_payment_gateways();
+		}
+
+		$this->assertNotWPError( $result );
+		$this->assertSame( array(), $result['gateways'] );
+		$this->assertSame( 0, $result['total'] );
+		$this->assertSame( 0, $result['per_page'] );
+		$this->assertSame( 0, $result['total_pages'] );
 	}

 	/**
@@ -5932,6 +5954,7 @@ class AbilitiesLoaderTest extends \WC_Unit_Test_Case {

 		$this->assertNotWPError( $result );
 		$this->assertSame( 'bacs', $result['gateways'][0]['id'] );
+		$this->assertSame( 1, $result['total'] );
 	}

 	/**
diff --git a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/OrdersQueryTest.php b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/OrdersQueryTest.php
index 7ed6edf1021..937dc617ef6 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/OrdersQueryTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/OrdersQueryTest.php
@@ -676,4 +676,117 @@ class OrdersQueryTest extends WC_Unit_Test_Case {

 		return $order->get_id();
 	}
+
+	/**
+	 * @testdox Should return the exact total of matching orders on partial, empty and out-of-range pages.
+	 *
+	 * @dataProvider storage_modes
+	 * @param bool $hpos Whether to use the custom orders table.
+	 */
+	public function test_returns_total_across_pages( bool $hpos ): void {
+		$this->use_storage( $hpos );
+		$order_ids = array();
+		foreach ( range( 1, 11 ) as $unused ) {
+			$order_ids[] = OrderHelper::create_order()->get_id();
+		}
+
+		$last = OrdersQuery::execute( array( 'page' => 2 ) );
+		$this->assertSame( 11, $last['total'] );
+		$this->assertSame( 2, $last['total_pages'] );
+		$this->assertCount( 1, $last['orders'] );
+
+		$beyond = OrdersQuery::execute( array( 'page' => 3 ) );
+		$this->assertSame( array(), $beyond['orders'] );
+		$this->assertSame( 11, $beyond['total'], 'An out-of-range page should keep the matching total.' );
+		$this->assertSame( 2, $beyond['total_pages'] );
+
+		$none = OrdersQuery::execute( array( 'status' => array( OrderStatus::REFUNDED ) ) );
+		$this->assertSame( 0, $none['total'] );
+		$this->assertSame( 0, $none['total_pages'] );
+		$this->assertSame( array(), $none['orders'] );
+		$none_beyond = OrdersQuery::execute(
+			array(
+				'status' => array( OrderStatus::REFUNDED ),
+				'page'   => 3,
+			)
+		);
+		$this->assertSame( 0, $none_beyond['total'] );
+		$this->assertSame( 0, $none_beyond['total_pages'] );
+
+		$id = OrdersQuery::execute( array( 'id' => $order_ids[0] ) );
+		$this->assertSame( 1, $id['total'] );
+		$this->assertSame( 1, $id['total_pages'] );
+
+		$schema = OrdersQuery::get_registration_args()['output_schema'];
+		$this->assertTrue( rest_validate_value_from_schema( $last, $schema, 'orders' ) );
+	}
+
+	/**
+	 * @testdox Should preserve filtered page sizes on full, partial and empty pages.
+	 *
+	 * @dataProvider storage_modes
+	 * @param bool $hpos Whether to use the custom orders table.
+	 */
+	public function test_pagination_follows_filtered_page_size( bool $hpos ): void {
+		$this->use_storage( $hpos );
+		for ( $i = 0; $i < 11; ++$i ) {
+			wc_create_order( array( 'status' => OrderStatus::PENDING ) );
+		}
+
+		$callback = static function ( $args ) {
+			if ( 10 === $args['limit'] ) {
+				$args['limit'] = 5;
+			}
+			return $args;
+		};
+		add_filter( 'woocommerce_order_query_args', $callback );
+		try {
+			foreach ( array(
+				1 => 5,
+				3 => 1,
+				4 => 0,
+			) as $page => $rows ) {
+				$result = OrdersQuery::execute(
+					array(
+						'page'     => $page,
+						'per_page' => 10,
+					)
+				);
+				$this->assertCount( $rows, $result['orders'] );
+				$this->assertSame( 11, $result['total'] );
+				$this->assertSame( 3, $result['total_pages'] );
+				$this->assertSame( 10, $result['per_page'] );
+			}
+		} finally {
+			remove_filter( 'woocommerce_order_query_args', $callback );
+		}
+	}
+
+	/**
+	 * @testdox Should narrow the total with extension query filters.
+	 *
+	 * @dataProvider storage_modes
+	 * @param bool $hpos Whether to use the custom orders table.
+	 */
+	public function test_total_follows_extension_query_filter( bool $hpos ): void {
+		$this->use_storage( $hpos );
+		$kept     = OrderHelper::create_order();
+		$excluded = OrderHelper::create_order();
+
+		$callback = static function ( $args ) use ( $excluded ) {
+			if ( OrderUtil::custom_orders_table_usage_is_enabled() ) {
+				$args['exclude'] = array( $excluded->get_id() );
+			} else {
+				$args['post__not_in'] = array( $excluded->get_id() );
+			}
+			return $args;
+		};
+		$hook     = $hpos ? 'woocommerce_order_query_args' : 'woocommerce_order_data_store_cpt_get_orders_query';
+		add_filter( $hook, $callback );
+		$result = OrdersQuery::execute( array() );
+		remove_filter( $hook, $callback );
+
+		$this->assertSame( 1, $result['total'] );
+		$this->assertSame( $kept->get_id(), $result['orders'][0]['id'] );
+	}
 }
diff --git a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ProductsQueryTest.php b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ProductsQueryTest.php
index 2e805dfb39e..10ecbcc9007 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ProductsQueryTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ProductsQueryTest.php
@@ -207,6 +207,102 @@ class ProductsQueryTest extends WC_Unit_Test_Case {
 		$this->assertArrayNotHasKey( 'low_stock_amount', $item );
 	}

+	/**
+	 * @testdox Should return the exact total of matching products on partial, empty and out-of-range pages.
+	 */
+	public function test_returns_total_across_pages(): void {
+		$product_ids = array();
+		foreach ( range( 1, 11 ) as $i ) {
+			$product_ids[] = WC_Helper_Product::create_simple_product( true, array( 'name' => "Total product {$i}" ) )->get_id();
+		}
+
+		$last = ProductsQuery::execute( array( 'page' => 2 ) );
+		$this->assertSame( 11, $last['total'] );
+		$this->assertSame( 2, $last['total_pages'] );
+		$this->assertCount( 1, $last['products'] );
+
+		$beyond = ProductsQuery::execute( array( 'page' => 3 ) );
+		$this->assertSame( array(), $beyond['products'] );
+		$this->assertSame( 11, $beyond['total'], 'An out-of-range page should keep the matching total.' );
+		$this->assertSame( 2, $beyond['total_pages'] );
+
+		$none = ProductsQuery::execute( array( 'search' => 'no-such-product-xyz' ) );
+		$this->assertSame( 0, $none['total'] );
+		$this->assertSame( 0, $none['total_pages'] );
+		$this->assertSame( array(), $none['products'] );
+		$none_beyond = ProductsQuery::execute(
+			array(
+				'search' => 'no-such-product-xyz',
+				'page'   => 3,
+			)
+		);
+		$this->assertSame( 0, $none_beyond['total'] );
+		$this->assertSame( 0, $none_beyond['total_pages'] );
+
+		$id = ProductsQuery::execute( array( 'id' => $product_ids[0] ) );
+		$this->assertSame( 1, $id['total'] );
+		$this->assertSame( 1, $id['total_pages'] );
+
+		$schema = ProductsQuery::get_registration_args()['output_schema'];
+		$this->assertTrue( rest_validate_value_from_schema( $last, $schema, 'products' ) );
+	}
+
+	/**
+	 * @testdox Should preserve filtered page sizes on full, partial and empty pages.
+	 */
+	public function test_pagination_follows_filtered_page_size(): void {
+		for ( $i = 0; $i < 11; ++$i ) {
+			WC_Helper_Product::create_simple_product();
+		}
+
+		$callback = static function ( $args ) {
+			if ( 10 === $args['posts_per_page'] ) {
+				$args['posts_per_page'] = 5;
+			}
+			return $args;
+		};
+		add_filter( 'woocommerce_product_data_store_cpt_get_products_query', $callback );
+		try {
+			foreach ( array(
+				1 => 5,
+				3 => 1,
+				4 => 0,
+			) as $page => $rows ) {
+				$result = ProductsQuery::execute(
+					array(
+						'page'     => $page,
+						'per_page' => 10,
+					)
+				);
+				$this->assertCount( $rows, $result['products'] );
+				$this->assertSame( 11, $result['total'] );
+				$this->assertSame( 3, $result['total_pages'] );
+				$this->assertSame( 10, $result['per_page'] );
+			}
+		} finally {
+			remove_filter( 'woocommerce_product_data_store_cpt_get_products_query', $callback );
+		}
+	}
+
+	/**
+	 * @testdox Should narrow the total with extension query filters.
+	 */
+	public function test_total_follows_extension_query_filter(): void {
+		$kept     = WC_Helper_Product::create_simple_product();
+		$excluded = WC_Helper_Product::create_simple_product();
+
+		$callback = static function ( $args ) use ( $excluded ) {
+			$args['post__not_in'] = array( $excluded->get_id() );
+			return $args;
+		};
+		add_filter( 'woocommerce_product_data_store_cpt_get_products_query', $callback );
+		$result = ProductsQuery::execute( array() );
+		remove_filter( 'woocommerce_product_data_store_cpt_get_products_query', $callback );
+
+		$this->assertSame( 1, $result['total'] );
+		$this->assertSame( $kept->get_id(), $result['products'][0]['id'] );
+	}
+
 	/**
 	 * @testdox Should match the output schema when listing products.
 	 */
diff --git a/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/ExtensibleAbilitiesTest.php b/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/ExtensibleAbilitiesTest.php
index ff40c3b899b..0c1f5e9baed 100644
--- a/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/ExtensibleAbilitiesTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/ExtensibleAbilitiesTest.php
@@ -323,6 +323,22 @@ class ExtensibleAbilitiesTest extends \WC_REST_Unit_Test_Case {
 			)
 		);

+		$beyond = $this->run_ability(
+			'woocommerce/products-query',
+			array(
+				'search'   => 'Pen',
+				'per_page' => 2,
+				'page'     => 3,
+				'filters'  => $filters,
+			)
+		);
+
+		$this->assertSame( 3, $page_one['total'] );
+		$this->assertSame( 3, $page_two['total'] );
+		$this->assertSame( 3, $beyond['total'] );
+		$this->assertSame( 2, $beyond['total_pages'] );
+		$this->assertSame( array(), $beyond['products'] );
+		$this->assertSame( 1, $plain['total'] );
 		$this->assertSame( 2, $page_one['total_pages'] );
 		$this->assertEqualsCanonicalizing( $with_code, array_merge( array_column( $page_one['products'], 'id' ), array_column( $page_two['products'], 'id' ) ) );
 		$this->assertSame( array( $without_code->get_id() ), array_column( $plain['products'], 'id' ) );
@@ -491,6 +507,18 @@ class ExtensibleAbilitiesTest extends \WC_REST_Unit_Test_Case {

 			$this->assertSame( $hpos, OrderUtil::custom_orders_table_usage_is_enabled() );
 			$this->assertSame( $sync, OrderUtil::is_custom_order_tables_in_sync() );
+			$beyond = $this->run_ability(
+				'woocommerce/orders-query',
+				array(
+					'per_page' => 1,
+					'page'     => 3,
+					'filters'  => array( 'extensions' => array( 'test-ext' => array( 'tag' => 'gift' ) ) ),
+				)
+			);
+			$this->assertSame( 2, $output['total'] );
+			$this->assertSame( 2, $beyond['total'] );
+			$this->assertSame( 2, $beyond['total_pages'] );
+			$this->assertSame( array(), $beyond['orders'] );
 			$this->assertSame( 2, $output['total_pages'] );
 			$this->assertContains( $output['orders'][0]['id'], $tagged );
 		} finally {