Skip to content

Commit 8230647

Browse files
committed
fix: 🐛 Fix the broken freshness-filter
It turns out that `wc_get_products()` discarded the meta_query, which lost the ability to filter out recently synced products, resulting in the ingestion loop trying to sync the same product batch over and over
1 parent 94b0b67 commit 8230647

2 files changed

Lines changed: 29 additions & 14 deletions

File tree

modules/ppcp-store-sync/src/Ingestion/IngestionBatchProvider.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,9 @@ private function collect_eligible( array $meta_query, array $current_batch = arr
126126
private function query_candidates( array $meta_query, array $exclude, int $limit, bool $order_by_meta ): array {
127127
// phpcs:disable WordPress.DB.SlowDBQuery -- intentionally using the meta_query here.
128128
$args = array(
129-
'limit' => $limit,
130-
'return' => 'ids',
131-
'meta_query' => array( $meta_query ),
132-
'exclude' => $exclude,
129+
'limit' => $limit,
130+
'return' => 'ids',
131+
'exclude' => $exclude,
133132
);
134133

135134
// Add ordering for stale products (oldest first).
@@ -142,7 +141,23 @@ private function query_candidates( array $meta_query, array $exclude, int $limit
142141
// Constrain to the coarse eligibility criteria (status/type/downloadable).
143142
$args = array_merge( $args, $this->product_filter->query_filters() );
144143

145-
$products = wc_get_products( $args );
144+
// wc_get_products() silently discards a raw 'meta_query' argument
145+
// (WC_Data_Store_WP::get_wp_query_args() skips it), so the freshness clause
146+
// is injected into the underlying WP_Query args through WooCommerce's own
147+
// query filter instead.
148+
$inject_meta_query = static function ( array $wp_query_args ) use ( $meta_query ): array {
149+
$wp_query_args['meta_query'][] = $meta_query;
150+
151+
return $wp_query_args;
152+
};
153+
154+
add_filter( 'woocommerce_product_data_store_cpt_get_products_query', $inject_meta_query );
155+
156+
try {
157+
$products = wc_get_products( $args );
158+
} finally {
159+
remove_filter( 'woocommerce_product_data_store_cpt_get_products_query', $inject_meta_query );
160+
}
146161
// phpcs:enable WordPress.DB.SlowDBQuery
147162

148163
assert( is_array( $products ) );

tests/PHPUnit/StoreSync/Ingestion/IngestionBatchProviderTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ public function test_get_batch_returns_never_synced_products_first(): void {
8383
} );
8484

8585
when( 'wc_get_products' )->alias( function ( $args ) use ( $never_synced_ids, $stale_ids ) {
86-
if ( 'NOT EXISTS' === $args['meta_query'][0]['compare'] ) {
86+
if ( ! isset( $args['orderby'] ) ) { // Fresh pass (the stale pass sets orderby).
8787
$available = array_values( array_diff( $never_synced_ids, $args['exclude'] ) );
8888
return array_slice( $available, 0, $args['limit'] );
8989
}
9090

91-
if ( '<' === $args['meta_query'][0]['compare'] ) {
91+
if ( isset( $args['orderby'] ) ) { // Stale pass (orders by the processed-at meta).
9292
$available = array_values( array_diff( $stale_ids, $args['exclude'] ) );
9393
return array_slice( $available, 0, $args['limit'] );
9494
}
@@ -119,7 +119,7 @@ public function test_get_batch_respects_limit_with_never_synced_products(): void
119119
} );
120120

121121
when( 'wc_get_products' )->alias( function ( $args ) use ( $never_synced_ids ) {
122-
if ( 'NOT EXISTS' === $args['meta_query'][0]['compare'] ) {
122+
if ( ! isset( $args['orderby'] ) ) { // Fresh pass (the stale pass sets orderby).
123123
$available = array_values( array_diff( $never_synced_ids, $args['exclude'] ) );
124124
return array_slice( $available, 0, $args['limit'] );
125125
}
@@ -146,7 +146,7 @@ public function test_get_batch_returns_stale_products_when_no_never_synced(): vo
146146
} );
147147

148148
when( 'wc_get_products' )->alias( function ( $args ) use ( $stale_product_ids ) {
149-
if ( '<' === $args['meta_query'][0]['compare'] ) {
149+
if ( isset( $args['orderby'] ) ) { // Stale pass (orders by the processed-at meta).
150150
$available = array_values( array_diff( $stale_product_ids, $args['exclude'] ) );
151151
return array_slice( $available, 0, $args['limit'] );
152152
}
@@ -192,12 +192,12 @@ public function test_get_batch_handles_mixed_results(): void {
192192
} );
193193

194194
when( 'wc_get_products' )->alias( function ( $args ) use ( $fresh_pool, $stale_pool ) {
195-
if ( 'NOT EXISTS' === $args['meta_query'][0]['compare'] ) {
195+
if ( ! isset( $args['orderby'] ) ) { // Fresh pass (the stale pass sets orderby).
196196
$available = array_values( array_diff( $fresh_pool, $args['exclude'] ) );
197197
return array_slice( $available, 0, $args['limit'] );
198198
}
199199

200-
if ( '<' === $args['meta_query'][0]['compare'] ) {
200+
if ( isset( $args['orderby'] ) ) { // Stale pass (orders by the processed-at meta).
201201
$available = array_values( array_diff( $stale_pool, $args['exclude'] ) );
202202
return array_slice( $available, 0, $args['limit'] );
203203
}
@@ -230,7 +230,7 @@ public function test_get_batch_stops_when_limit_reached_after_fresh_products():
230230
} );
231231

232232
when( 'wc_get_products' )->alias( function ( $args ) use ( $fresh_pool ) {
233-
if ( 'NOT EXISTS' === $args['meta_query'][0]['compare'] ) {
233+
if ( ! isset( $args['orderby'] ) ) { // Fresh pass (the stale pass sets orderby).
234234
$available = array_values( array_diff( $fresh_pool, $args['exclude'] ) );
235235
return array_slice( $available, 0, $args['limit'] );
236236
}
@@ -273,7 +273,7 @@ public function test_get_batch_terminates_and_skips_unloadable_product_id(): voi
273273
when( 'wc_get_products' )->alias( function ( $args ) use ( $fresh_pool, &$call_count ) {
274274
$call_count++;
275275

276-
if ( 'NOT EXISTS' === $args['meta_query'][0]['compare'] ) {
276+
if ( ! isset( $args['orderby'] ) ) { // Fresh pass (the stale pass sets orderby).
277277
$available = array_values( array_diff( $fresh_pool, $args['exclude'] ) );
278278
return array_slice( $available, 0, $args['limit'] );
279279
}
@@ -330,7 +330,7 @@ function ( $product ) use ( $products ) {
330330
$fresh_pool = array( 1, 2, 3 );
331331

332332
when( 'wc_get_products' )->alias( function ( $args ) use ( $fresh_pool ) {
333-
if ( 'NOT EXISTS' === $args['meta_query'][0]['compare'] ) {
333+
if ( ! isset( $args['orderby'] ) ) { // Fresh pass (the stale pass sets orderby).
334334
$available = array_values( array_diff( $fresh_pool, $args['exclude'] ) );
335335
return array_slice( $available, 0, $args['limit'] );
336336
}

0 commit comments

Comments
 (0)