diff --git a/src/Product/ProductRepository.php b/src/Product/ProductRepository.php index 2e9bea0311..24089bd469 100644 --- a/src/Product/ProductRepository.php +++ b/src/Product/ProductRepository.php @@ -88,6 +88,11 @@ public function find_ids( array $args = [], int $limit = -1, int $offset = 0 ): * @return WC_Product[] Array of WooCommerce product objects */ public function find_by_ids( array $ids, array $args = [], int $limit = -1, int $offset = 0 ): array { + // If no product IDs are supplied then return early to avoid querying and loading every product. + if ( empty( $ids ) ) { + return []; + } + $args['include'] = $ids; return $this->find( $args, $limit, $offset ); diff --git a/tests/Unit/Product/ProductRepositoryTest.php b/tests/Unit/Product/ProductRepositoryTest.php index 852a50fb9a..3a49a56257 100644 --- a/tests/Unit/Product/ProductRepositoryTest.php +++ b/tests/Unit/Product/ProductRepositoryTest.php @@ -145,6 +145,8 @@ public function test_find_by_ids() { array_map( 'wc_get_product', $ids ), $this->product_repository->find_by_ids( $ids ) ); + + $this->assertEquals( [], $this->product_repository->find_by_ids( [] ) ); } public function test_find_synced_products() {