From 5a0b5ef49eedad151c01bb30671ba284607cb7bf Mon Sep 17 00:00:00 2001 From: martynmjones Date: Fri, 16 Feb 2024 14:11:54 +0000 Subject: [PATCH 1/2] Return empty array if no ids are supplied --- src/Product/ProductRepository.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Product/ProductRepository.php b/src/Product/ProductRepository.php index 2add3bb281..ef1cc5e1a9 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 ); From cfa8fb1f340b5ac6b2aa2cbd4003c8131ecda9d4 Mon Sep 17 00:00:00 2001 From: martynmjones Date: Fri, 16 Feb 2024 14:24:28 +0000 Subject: [PATCH 2/2] Add unit test --- tests/Unit/Product/ProductRepositoryTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/Unit/Product/ProductRepositoryTest.php b/tests/Unit/Product/ProductRepositoryTest.php index 05759d25cd..01b2afa9c8 100644 --- a/tests/Unit/Product/ProductRepositoryTest.php +++ b/tests/Unit/Product/ProductRepositoryTest.php @@ -144,6 +144,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() {