Skip to content

Commit

Permalink
Merge pull request #2260 from woocommerce/fix/1959-prevent-find-by-id…
Browse files Browse the repository at this point in the history
…s-executing-without-ids-supplied

Prevent find_by_ids executing if no arguments are supplied
  • Loading branch information
martynmjones authored Feb 16, 2024
2 parents 11cdc14 + cfa8fb1 commit e9bc80d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Product/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/Product/ProductRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit e9bc80d

Please sign in to comment.